Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to convert lowercase string to uppercase using PHP ?

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.

Approach 1: Using chr() method: 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




<?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

Approach 2: Using strtoupper() method: The strtoupper() is an inbuilt method in PHP which carries out case-conversion. The alphabetics are all converted to upper case, and returned to the form of a string. The numbers and special symbols remain unmodified. The result has to be saved in a variable in order to preserve the changes. This method is faster and optimized in comparison to the previous approach.

strtoupper ( $string ) 

Arguments : 

$str – The string to convert case of 

Return Type : 

Returns a string with all upper case characters. 

PHP




<?php
$str = "Geeks^for+Geeks";
print ("Original String \n");
print ($str. "\n");
$cap_str = strtoupper($str);
print ("Uppercase String \n");
print ($cap_str);
  
?>


Output

Original String 
Geeks^for+Geeks
Uppercase String 
GEEKS^FOR+GEEKS

RELATED ARTICLES

Most Popular

Recent Comments