PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, you can use several built-in functions to change the case of strings. Here are some common functions:
strtoupper()
: This function converts a string to all uppercase letters.$string = "Hello, World!"; $upper = strtoupper($string); echo $upper; // Outputs: "HELLO, WORLD!"
strtolower()
: This function converts a string to all lowercase letters.$string = "Hello, World!"; $lower = strtolower($string); echo $lower; // Outputs: "hello, world!"
ucfirst()
: This function converts the first character of a string to uppercase.$string = "hello, world!"; $firstUpper = ucfirst($string); echo $firstUpper; // Outputs: "Hello, world!"
lcfirst()
: This function converts the first character of a string to lowercase.$string = "Hello, world!"; $firstLower = lcfirst($string); echo $firstLower; // Outputs: "hello, world!"
ucwords()
: This function converts the first character of each word in a string to uppercase.$string = "hello, world!"; $wordsUpper = ucwords($string); echo $wordsUpper; // Outputs: "Hello, World!"
Remember that these functions return a new string with the converted case, they do not modify the original string. This is because strings in PHP are immutable, meaning they cannot be changed after they are created.
Convert string to uppercase in PHP:
strtoupper()
function to convert a string to uppercase.// File: uppercase_conversion.php $originalString = "hello world"; $uppercaseString = strtoupper($originalString); echo "Uppercase: $uppercaseString"; // Output: Uppercase: HELLO WORLD
PHP strtolower()
and strtoupper()
functions:
strtolower()
converts a string to lowercase, and strtoupper()
converts it to uppercase.// File: case_conversion_functions.php $originalString = "Hello World"; $lowercaseString = strtolower($originalString); $uppercaseString = strtoupper($originalString); echo "Lowercase: $lowercaseString, Uppercase: $uppercaseString"; // Output: Lowercase: hello world, Uppercase: HELLO WORLD
Change case of the first letter in PHP:
ucfirst()
to change the case of the first letter in a string.// File: first_letter_case.php $originalString = "hello world"; $modifiedString = ucfirst($originalString); echo "Modified: $modifiedString"; // Output: Modified: Hello world
Title case conversion in PHP:
ucwords()
to capitalize the first letter of each word.// File: title_case.php $originalString = "hello world"; $titleCaseString = ucwords($originalString); echo "Title Case: $titleCaseString"; // Output: Title Case: Hello World
Case-insensitive string comparison in PHP:
strcasecmp()
for case-insensitive string comparison.// File: case_insensitive_comparison.php $string1 = "Hello"; $string2 = "hello"; $comparisonResult = strcasecmp($string1, $string2); echo "Comparison Result: $comparisonResult"; // Output: Comparison Result: 0 (equal)
PHP ucwords()
function example:
ucwords()
to capitalize the first letter of each word.// File: ucwords_example.php $originalString = "the quick brown fox"; $capitalizedString = ucwords($originalString); echo "Capitalized: $capitalizedString"; // Output: Capitalized: The Quick Brown Fox
Toggle case in PHP string:
strtolower()
and strtoupper()
to toggle the case of each character.// File: toggle_case.php $originalString = "Hello World"; $toggledString = implode(array_map('strtolower', str_split($originalString))); echo "Toggled Case: $toggledString"; // Output: Toggled Case: hELLO wORLD
Convert string to camel case in PHP:
// File: camel_case_conversion.php function toCamelCase($string) { $words = explode(' ', ucwords(str_replace('_', ' ', $string))); $firstWord = lcfirst(array_shift($words)); return $firstWord . implode('', $words); } $snakeCaseString = "hello_world_example"; $camelCaseString = toCamelCase($snakeCaseString); echo "Camel Case: $camelCaseString"; // Output: Camel Case: helloWorldExample
PHP mb_convert_case()
for multibyte strings:
mb_convert_case()
for multibyte strings to handle characters from various languages.// File: mb_convert_case_example.php $multibyteString = "����ˤ��� ����"; $convertedString = mb_convert_case($multibyteString, MB_CASE_UPPER, "UTF-8"); echo "Converted: $convertedString"; // Output: Converted: ����ˤ��� ���� (in uppercase)