PHP | String Functions

We have learned about some basic string manipulation functions available in PHP in the article PHP | String . In this article, we will learn about few string functions that are used to change cases of characters of strings in PHP. Below are some most commonly used case manipulation functions for strings in PHP:

strtoupper() function in PHP

This function takes a string as argument and returns the string with all characters in Upper Case.



Syntax:

strtoupper($string)

Program to illustrate the use of strtoupper() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
# PHP code to convert to Upper Case
function toUpper($string){
    return(strtoupper($string));
}
  
// Driver Code
$string="GeeksforGeeks";  
echo (toUpper($string));
?>  

chevron_right


Output:

GEEKSFORGEEKS

strtolower() function in PHP

This function takes a string as argument ans returns the string with all of the characters in Lower Case.

Syntax:

strtolower($string)

Program to illustrate the use of strtolower() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
# PHP code to convert to Lower Case
function toLower($string){
    return(strtolower($string));
}
  
// Driver Code
$string="GeeksforGeeks";  
echo (toLower($string));
?>  

chevron_right


Output:

geeksforgeeks

ucfirst() function in PHP

This function takes a string as argument and returns the string with the first character in Upper Case and all other cases of the characters remains unchanged.


Syntax:

ucfirst($string)

Program to illustrate the use of ucfirst() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
# PHP code to convert the first letter to Upper Case
function firstUpper($string){
    return(ucfirst($string));
}
  
// Driver Code
$string="welcome to GeeksforGeeks";  
echo (firstUpper($string));
?>

chevron_right


Output:

Welcome to GeeksforGeeks

lcfirst() function in PHP

This function takes a string as argument and returns the string with the first character in Lower Case and all other characters remains unchanged.

Syntax:

lcfirst($string)

Program to illustrate the use of lcfirst() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
# PHP code to convert the first letter to Lower Case
function firstLower($string){
    return(lcfirst($string));
}
  
// Driver Code
$string="WELCOME to GeeksforGeeks";  
echo (firstLower($string));
?>

chevron_right


Output:

wELCOME to GeeksforGeeks

ucwords() function in PHP

This function takes a string as argument and returns the string with the first character of every word in Upper Case and all other characters remains unchanged.


Syntax:

ucwords($string)

Program to illustrate the use of ucwords() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
# PHP code to convert the first letter 
# of each word to Upper Case
function firstUpper($string){
    return(ucwords($string));
}
  
// Driver Code
$string="welcome to GeeksforGeeks";  
echo (firstUpper($string));
?>

chevron_right


Output:

Welcome To GeeksforGeeks

strlen() function in PHP

This function takes a string as argument and returns and integer value representing the length of string. It calculates the length of the string including all the whitespaces and special characters.

Syntax:

strlen($string)

Program to illustrate the use of strlen() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
# PHP code to get the length of any string
function Length($string){
    return(strlen($string));
}
  
// Driver Code
$string="welcome to GeeksforGeeks";  
echo (Length($string));
?>

chevron_right


Output:

24


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.