How to use chr() method In PHP
This approach uses a variety of in-built PHP methods to convert a string to upper case characters. Initially, a string is declared consisting of characters, numbers or special symbols. The str_split() method is used to convert the string into an array of individual characters. The characters are mapped to specific indexes together to form a character array.
A for loop iteration is then performed over this array. Each character is validated, to verify if is a lower case alphabetic character. The ctype_lower() method is used to return a boolean value depending on which category the character belongs. This method returns TRUE, if the specified argument character is in lower case, else returns FALSE.
ctype_lower(ch)
In case, this method returns FALSE, the character is either a non-alphabetic character or an upper case character. If this scenario arises, the character is displayed unmodified. Else, the particular character is converted to upper case character, by subtracting ’32’ from its ASCII value.
ord (ch ) - 32
This integer value, is then converted to its corresponding character value using the chr() method.
The time complexity required for this approach’s execution is O(l), where l is the length of the string.
<?php
// Declare string
$str = "Geeks^for+Geeks";
print ("Original String \n");
// Split string in characters
$chars = str_split($str);
print ($str. "\n");
print ("Uppercase String \n");
// Looping over characters
foreach ($chars as $ch){
// Check if character is
// small case alphabet
if(ctype_lower($ch)){
// Convert to upper case
echo chr(ord($ch)-32);
}
else{
// Else print character
// unmodified
echo($ch);
}
}
?>
Output
Original String Geeks^for+Geeks Uppercase String GEEKS^FOR+GEEKS
How to convert lowercase string to uppercase using PHP ?
A string is a storage object in PHP comprising characters, numbers or special symbols. Strings in PHP are case-sensitive. The interconversion between lower and upper case can be easily done using various in-built methods in PHP.