Some functions are very common and we use them frequently in our projects. No matter which CMS or Framework we are using but as long as they are based on PHP we can utilize these PHP functions to perform some basic tasks.
implode() in PHP
The implode() function converts an array into an string and separate each element with the separator string provided as second parameter.
<?php $fruits= array(); $fruits = "Mango"; $fruits = "Apple"; $fruits = "Banana"; $fruits = "Oranges"; echo implode($fruits,",");// prints Mango,Apple,Banana,Oranges ?>
explode() in PHP
PHP explode() function breaks a string based on the delimiter and returns an array.
- Delimiter required
This is the separater string
- String required
This is the string which will be converted into array
- Limit optional
If provided it will limit the elements of the array and last element will contain rest of the string.
<?php $string = "Mango,Oranges,Banana,Apple"; $exploded_array = explode(",",$string); print_r($exploded_array);//output will be Array (  => Mango  => Oranges  => Banana  => Apple ) ?>
Following code uses limit parameter as well.
<?php $string = "Mango,Oranges,Banana,Apple"; $exploded_array = explode(",",$string,2); print_r($exploded_array);//output will be Array (  => Mango  => Oranges,Banana,Apple ) ?>
lcfirst() in PHP
The lcfirst() function in PHP makes first character of the string lowercase.
string The string
string returned string with first character lowercased.
<?php $string = "THIS IS CAPITAL";// prints "tHIS IS CAPITAL" echo lcfirst($string); ?>
str_replace() in PHP
The str_replace() function replaces the searched string in a string. search string and replace string is passed as first and second argument while the third argument is string itself.
We can supply string or array as arguments for search and replace, let’s have a look how the function performs in both situtations.
<?php $string = "This is my first string"; echo str_replace("my","your",$string);// will print "This is your first string" echo str_replace(array('my','first'),array('your','second'),$string);// will print "This is your second string" ?>
strlen() in PHP
The Strlen() function returns the number of characters in a string. This function is useful in validations and mostly used in string manipulations in combination with other string functions of PHP.
<?php $message = "Hello world"; echo strlen($message); //this will echo 11 ?>
strlen() function takes string as argument and return integer.
strlen() also counts spaces.