Till now we have learnt what is function? And how can we define user defined functions and how can we execute them? Now we will learn some of the inbuilt functions used in PHP. PHP provides rich set of inbuilt functions which we can use whenever required. As its inbuilt function we need not to define it, it’s already defined all we need to do is just call them whenever required.
Let’s learn the first type of inbuilt function that is String Functions. A string function is used for manipulating strings. There are various string functions which are used to perform some operations with strings. The different functions are as listed below.
This function is used to find the length of given string.
Syntax
int strlen(string)
For Ex.
<?php $str="DiscussDesk"; echo "Length of ".$str." is ".strlen($str); ?>
Output
Length of DiscussDesk is 11
This function is used for reversing a given string.
Syntax
string strrev(string)
For Ex.
<?php $str="DiscussDesk"; echo strrev($str); ?>
Output
kseDssucsiD
This function is used to convert the ASCII code values into character value.
Syntax
chr(ASCII Code in integer)
This function is opposite of chr() it takes a character as input and converts it to associated ASCII value.
Syntax
ord(character)
For Ex.
<?php echo chr(65)."<br>"; // it will print A as its ASCII code is 65 echo ord('A'); // it will print 65 ?>
This function returns all characters of strings into lower case.
Syntax
string strtolower(string)
This function returns all characters of strings into upper case.
Syntax
string strtoupper(string)
This function will convert the first character into capital letter and rest characters into lower case. In short it will capitalize the first character of given string.
Syntax
string ucfirst(string)
This function will convert the first letter of each word into capital letter(upper case).
Syntax
string ucwords(string)
For Ex.
<?php $str="hello discussDesk"; echo "After strtolower <b>".strtolower($str)."<br></b>"; echo "After strtoupper <b>".strtoupper($str)."<br></b>"; echo "After ucfirst <b>".ucfirst($str)."<br></b>"; echo "After ucwords <b>".ucwords($str)."<br></b>"; ?>
Output
After strtolower hello discussdesk After strtoupper HELLO DISCUSSDESK After ucfirst Hello discussDesk After ucwords Hello DiscussDesk
It is abbreviated as left trim. This function is used to remove the unnecessary white spaces from beginning (left) of the string.
Syntax
string ltrim(string variable[, string character])
The first parameter is the variable name for which you need to remove the white spaces. The second parameter is optional and it is used to specify the character/s which are to be removed from the left side of the string.
For Ex.
<?php $str1=" DiscussDesk"; $str2="DiscussDesk"; echo ltrim($str1)."<br>"; echo ltrim($str2,"Di"); ?>
Output
DiscussDesk scussDesk
It is abbreviated as right trim. This function is used to remove the unnecessary white spaces from end (right) of the string.
Syntax
string rtrim(string variable[, string character])
The first parameter is the variable name for which you need to remove the white spaces from right side of your string. The second parameter is optional and it is used to specify the character/s which are to be removed from the end (right side) of the string.
For Ex.
<?php $str1="DiscussDesk "; $str2="DiscussDesk"; echo rtrim($str1)."<br>"; echo rtrim($str2,"Desk"); ?>
Outputp
DiscussDesk Discu
Note :- if you have noticed the output of rtrim($str2,”Desk”) , it will remove all occurrences of characters ‘D’ , ‘e’ , ‘s’ , ‘k’ regardless of their order so it will not remove string “Desk” instead it will remove all occurrences of characters ‘D’ , ‘e’ , ‘s’ , ‘k’ from the right side of string. Same is the case with ltrim().
This function is used to remove the unnecessary white spaces from beginning (left) of the string as well as from the end (right side) of the string.
Syntax
string trim(string variable[, string character])
The first parameter is the variable name for which you need to remove the white spaces from both ends of string. The second parameter is optional and it is used to specify the character/s which are to be removed from the end (right side) of the string as well as the beginning (left side) of the string.
For Ex.
<?php $str1=" DiscussDesk "; $str2="***DiscussDesk***"; echo trim($str1)."<br>"; echo trim($str2,"*"); ?>
Output
DiscussDesk DiscussDesk
So for now we have discussed some of the string functions which are used very frequently, we will discuss more of them later in our next tutorial.