String Function
String Function
Syntax
Example
<?php
$str="My name is KHAN";
$str=strtolower($str);
echo $str;
?>
Output:
my name is khan
Syntax
Example
<?php
$str="My name is KHAN";
$str=strtoupper($str);
echo $str;
?>
Output:
MY NAME IS KHAN
The ucfirst() function returns string converting first character into uppercase. It doesn't change the
case of other characters.
Syntax
Example
<?php
$str="my name is KHAN";
$str=ucfirst($str);
echo $str;
?>
Output:
My name is KHAN
The lcfirst() function returns string converting first character into lowercase. It doesn't change the
case of other characters.
Syntax
Example
<?php
$str="MY name IS KHAN";
$str=lcfirst($str);
echo $str;
?>
Output:
mY name IS KHAN
The ucwords() function returns string converting first character of each word into uppercase.
Syntax
Example
<?php
$str="my name is Sonoo jaiswal";
$str=ucwords($str);
echo $str;
?>
Output:
Syntax
Example
<?php
$str="my name is Sonoo jaiswal";
$str=strrev($str);
echo $str;
?>
Output:
Syntax
Example
<?php
$str="my name is Sonoo jaiswal";
$str=strlen($str);
echo $str;
?>
Output:
24
Comparing two strings is one of the most commonly used string operation in programming and web
development practices. The strcmp() is an inbuilt function in PHP and is used to compare two
strings. This function is case-sensitive which points that capital and small cases will be treated
differently, during comparison. This function compares two strings and tells us that whether the first
string is greater or smaller than the second string or equals to the second string.
Syntax:
strcmp($string1, $string2)
Parameters: This function accepts two parameters which are described below:
$string1 (mandatory): This parameter refers to the first string to be used in the comparison
$string2 (mandatory): This parameter refers to the second string to be used in the comparison.
Return Values: The function returns a random integer value depending on the condition of match,
which is given by:
Example
<?php
?>
Output:
0
31
-31