Friday, December 27, 2024
Google search engine
HomeLanguagesPHP | String Functions

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:




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


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:




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


Output:

neveropen

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:




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


Output:

Welcome to neveropen

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:




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


Output:

wELCOME to neveropen

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:




<?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 neveropen";  
echo (firstUpper($string));
?>


Output:

Welcome To neveropen

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:




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


Output:

24

RELATED ARTICLES

Most Popular

Recent Comments