PHP has very large library of built-in functions which are available to use. Here in this post, I have listed some most used PHP functions which will surely help you.
Also, read:
- 15+ regular expressions for PHP developers
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
1. htmlentities()
This PHP function is used to convert all applicable characters to HTML entities. it has four parameters named as String, Flags, Encoding and double_encode. The first parameter gets input as string. Second parameter takes flag to specify how to handle quotes. The default flag is ENT_COMPAT | ENT_HTML401. Remaining parameters are optional. This function is useful to protect form post data from malicious content. Actually when user submits form data, the form post values passed by htmlentities() function to converts applicable characters into html entities.
Example:
<?php $str = "A quote is <b>bold</b>"; echo htmlentities($str); ?>
2. Htmlspecialchars()
Actually certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with these conversions made. If you require all input substrings that have associated named entities to be translated, use htmlentities() .
Example:
<?php $str = htmlspecialchars("<a href='test>Test</a>”, ENT_QUOTES); echo $str; ?>
3. strip_tags
This function is identical to htmlentities() function except strip_tags function strips html tags and html from a string. The function has two parameters named as String allowable tags. If we pass allowable tags then the returned string printed with allowed html tags.
Example:
<?php $text = '<p>Test String.</p>'; echo strip_tags($text); ?>
If passed allowed tags value then the passed tags not stripped.
<?php $text = '<p>Test String.</p>'; echo strip_tags($text,'<p>'); ?>
4. addslashes
addslashes function is used to quote string with slashes. It returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (‘), double quote (“), backslash (\) and NUL (the NULL byte).
Example:
<?php $str = “Is your name O’reilly?”; echo addslashes($str); // Outputs: Is your name O\’reilly? ?>
5. stripslashes
The stripslashes function is used to un-quotes a quoted string.
Example:
<?php $str = “Is your name O\’reilly?”; echo stripslashes($str); // Outputs: Is your name O’reilly? ?>
6. explode
The explode function take its input as string and convert it into array. The function takes first parameter as separator and second one is string. It converts string into array based on separator.
Example:
<?php $pizza = “piece1 piece2 piece3 piece4 piece5 piece6”; $pieces = explode(” “, $pizza); // ssparate by space echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 ?>
7- implode
This function is just opposite to explode function. As explode function converts string into array, but the implode function take its input as array and convert it into string.
Example:
<?php $array = array(‘lastname’, ’email’, ‘phone’); $comma_separated = implode(“,”, $array); echo $comma_separated; //Output: lastname,email,phone ?>
8- rand()
The rand() function outputs a random number. It doesn’t require any parameter but there are two option parameters that you can add to set mi and max for the number.
Example:
<?php echo rand(); echo rand(10,20); echo rand(0,1); echo rand(1000,2000); ?>
9- str_replace
str_replace() is useful when you want to do a simple match and replace. This function accepts four parameters, three required. find, replace, string, count(optional).
Example:
<?php $phone = ‘222-333-5555-66666’; echo str_replace(‘-‘, ”, $phone); //Output: 222333555566666 ?>
10- date()
Working with dates in PHP is very easy. The date() function can have two parameters, format(required) and timestamp(optional).
Example:
<?php echo date(“d-m-Y h:i:s”); //prints the current date time ?>
11. strlen()
The strlen function will return the length of a given string. The spaces and characters are added to the count.
Example:
<?php $firstString = ‘Hello PHP?’; echo strlen($firstString); // prints 10 $secondString = ‘ hello code’; echo strlen($secondString); // prints 11 ?>
12. count()
This function count the number of element in a array.
Example:
<?php $myarray = array(“One”,“Two”,“Three”); echo count($myarray); // prints 3 ?>
13. array_combine
This function creates an array by using one array for keys and another for its values.
Example:
<?php $a = array(‘green’, ‘red’, ‘yellow’); $b = array(‘avocado’, ‘apple’, ‘banana’); $c = array_combine($a, $b); print_r($c); ?>
The above example will output:
Array ( [green] => avocado [red] => apple [yellow] => banana )
14. array_multisort
It is used to sort multiple or multi-dimensional arrays.
Example:
<?php $ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); var_dump($ar1); var_dump($ar2); ?>
The above example will output:
array(4) { [0]=> int(0) [1]=> int(10) [2]=> int(100) [3]=> int(100) } array(4) { [0]=> int(4) [1]=> int(1) [2]=> int(2) [3]=> int(3) }
15. array_unique
You can use this function to removes duplicate values from an array.
Example:
<?php $input = array(“a” => “green”, “red”, “b” => “green”, “blue”, “red”); $result = array_unique($input); print_r($result); ?>
The above example will output:
Array ( [a] => green [0] => red [1] => blue )
16. print_r
This function is used to print array variable in a way that’s readable by humans.
Example:
<?php $fruits = array (‘a’ => ‘Banana’, ‘b’ => ‘Apple’); print_r ($fruits); ?>
The results would be something like:
Array ( [a] => Banana [b] => Apple )
This is a very short list of PHP’s built in functions. For all PHP built in functions list, go to PHP.NET functions list page.
You may also like:
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Manipulating PHP arrays: push, pop, shift, unshift
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 Most Important Directory Functions in PHP
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably
This is very good Article. Everyone can learn many things from this article.