0% found this document useful (0 votes)
34 views6 pages

PHP String Function

Php course with code

Uploaded by

amanraz0709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

PHP String Function

Php course with code

Uploaded by

amanraz0709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PHP String Function

PHP strtolower() function:


The strtolower() function returns string in lowercase letter.

Example:
<?php
$str="My name is CSE";
$str=strtolower($str);
echo $str;
?>
Output:
my name is cse

PHP strtoupper() function:


The strtoupper() function returns string in uppercase letter.

Example:
<?php
$str="My name is php";
$str=strtoupper($str);
echo $str;
?>
Output:
MY NAME IS PHP

PHP ucfirst() function:


The ucfirst() function returns string converting first character into uppercase. It doesn't change
the case of other characters.

Example:
<?php
$str="my name is PHP";
$str=ucfirst($str);
echo $str;
?>
Output:
My name is PHP

PHP lcfirst() function:


The lcfirst() function returns string converting first character into lowercase. It doesn't change
the case of other characters.

Example:
<?php
$str="MY name IS PHP";
$str=lcfirst($str);
echo $str;
?>
Output:
mY name IS PHP

PHP ucwords() function:


The ucwords() function returns string converting first character of each word into uppercase.

Example:
<?php
$str="my name is cse third year";
$str=ucwords($str);
echo $str;
?>
Output:
My Name Is Cse Third Year

PHP strrev() function:


The strrev() function returns reversed string.

Example:
<?php
$str="my name is cse third year";
$str=strrev($str);
echo $str;
?>
Output:
raey driht esc si eman ym

PHP strlen() function:


The strlen() function returns length of the string.

Example:
<?php
$str="my name is cse third year";
$str=strlen($str);
echo $str;
?>
Output:
25

PHP chop() Function:


The chop() function is used to remove whitespace or other predefined character from the right
end of a string.
Example:
<?php
$str = "Hello World!";
echo "Your string is :".$str."<br>";
echo "By using 'chop()' Functions is your string is: ".chop($str,"World!");
?>

Output:
Your string is :Hello World!
By using 'chop()' Functions is your string is: Hello

PHP chunk_split() Function:


PHP chunk_split() function is used to splits a string into smaller parts or chunks. The function
does not alter the original string.

Example:
<?php
$str = "Hello CSE3rdYear!";
echo "Your string is:".$str;
echo "</br>";
echo "By using 'chunk_split()' function your string is:".chunk_split($str,6,"...");
?>

Output:
Your string is:Hello CSE3rdYear!
By using 'chunk_split()' function your string is:Hello ...CSE3rd...Year!...

PHP count_chars()function:
PHP count_chars() is most important string function. It is used to return information about
characters in a string.
Note:
3 : a string containing all unique characters is returned.
4 : a string containing all not used characters is returned.

Example-1
<?php
$str = "cse branch!";
echo "Your given string: ".$str;
echo "<br>"."By using 'count_chars()' function your string is :".count_chars($str,3);
?>

Output:
Your given string: cse branch!
By using 'count_chars()' function your string is : !abcehnrs
Example-2
<?php
$str = "cse branch!";
echo "Your given string: ".$str;
echo "<br>"."By using 'count_chars()' function your string is :".count_chars($str,4);
?>

Output:
Your given string: cse branch!
By using 'count_chars()' function your string is :
-"#$%&'()*+,-
./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`dfgijklmopqtuvwxyz{
|}~����

PHP string strcasecmp() function:


It is used to compare two given string. It is binary safe and case-insensitive.

It returns:
o If the two strings are equal: [ 0 ]
o If string1 is less than string2: [< 0]
o If string1 is greater than string2: > 0

Example:
<?php
$str1 = "helloCSE";
$str2 = "HELLOcse";
echo "Your first string is:".$str1;
echo "<br>";
echo "Your second string is:".$str2;
echo "<br>";
echo strcasecmp("$str1","$str2");
?>

Output:
Your first string is:helloCSE
Your second string is:HELLOcse
0

PHP string strchr() Function:


It is the alias of strstr() function or can used to search for the first occurrence of a given
string.

Example:
<?php
$str1="Hello CSE";
$str2="CSE";
echo strchr($str1,$str2);
?>

Output:
CSE

Example:
<?php
$str1="Hello CSE";
$str2="CSE";
echo strchr($str1,"e");
?>

Output:
ello CSE

PHP string strcmp() Function:


strcmp() is a string comparison function in PHP. It is a built-in function of PHP, which is case
sensitive, means it treats capital and the small case separately. It is used to compare two
strings from each other.
It returns:
Return 0 - It returns 0 if both strings are equal, i.e., $str1 = $str2
Return < 0 - It returns negative value if string1 is less than string2, i.e., $str1 < $str2
Return >0 - It returns positive value if string1 is greater than string 2, i.e., $str1 > $str2

Example:
<?php
echo strcmp("Hello ", "HELLO"). " because the first string is greater than the second
string.";
echo "</br>";
echo strcmp("Hello cse", "Hello cse Hello"). " because the first string is less than the
second string.";
?>

Output:
8192 because the first string is greater than the second string.
-6 because the first string is less than the second string.

Note:

String1 String2 Output Explanation

Hello Hello 0 Both strings are same and equal.


Hello hello -1 String1 < String2 because ASCII value of H is 72 and h is
104 so that H < h. It treats the small and capital letters
differently.

hello Hello 1 String1 > String2 because ASCII value of H is 72 and h is


104 so that H < h.

Hello Hello 4 String1 > String2 because of String1 greater than String2
PHP by 6 characters including whitespace.

hello Hello 1 String1 > String2 because ASCII value of H is 72 and h is


PHP 104 so that H < h.

Hello Hello -4 String1 < String2 because String1 is lesser than String2 by
PHP 4 characters including whitespace.

You might also like