In this tutorial we will discover the list of the 100 most used native functions in PHP.
1. count
Returns the number of elements in an array.
1 2 3 4 5 6 | <?php $colors=array("Blue","Orange","red"); echo count($colors); ?> |
Output:
1 2 3 | 3 |
2. is_array
Is used to determine whether a variable is an array or not.
1 2 3 4 5 6 7 8 9 | <?php $tab = array('X','Y','X'); if (is_array($tab)) echo "This is an array...."; else echo "This is not a table...."; ?> |
Output:
1 2 3 | This is an array.... |
3. substr
Returns part of a string.
1 2 3 4 5 | <?php echo substr("Welcom to Code4Example", 10); ?> |
Output:
1 2 3 | Code4Example |
4. in_array
Finds a specific value in an array.
1 2 3 4 5 6 7 8 9 10 11 | <?php $colors = array("Blue", "Orange", "Red"); if (in_array("Red", $colors)) { echo "Red was found"; } if (in_array("Green", $colors)) { echo "Green was found"; } ?> |
Output:
1 2 3 | Red was found |
5. explode
Divide a string into an array.
1 2 3 4 5 6 | <?php $str = "Welcom to Code4Example"; print_r (explode(" ",$str)); ?> |
Output:
1 2 3 4 5 6 7 8 | Array ( [0] => Welcom [1] => to [2] => Code4Example ) |
6. implode
Returns a string from the elements of an array.
1 2 3 4 5 6 | <?php $arr = array('Hello', 'World!', 'How', 'are', 'you?'); echo implode(" ",$arr); ?> |
Output:
1 2 3 | Hello World! How are you? |
7. str_replace
Divide a string into an array.
1 2 3 4 5 | <?php echo str_replace("Welcom", "Hello", "Welcom to Code4Example!"); ?> |
Output:
1 2 3 | Hello to Code4Example! |
8. strlen
Returns the length of a string.
1 2 3 4 5 | <?php echo strlen("Code4Example"); ?> |
Output:
1 2 3 | 12 |
9. array_merge
Merges one or more tables into a single table.
1 2 3 4 5 6 7 | <?php $c1=array("blue","orange"); $c2=array("green","red"); print_r(array_merge($c1,$c2)); ?> |
Output:
1 2 3 4 5 6 7 8 9 | Array ( [0] => blue [1] => orange [2] => green [3] => red ) |
10. strpos
Finds the position of the first occurrence of one string within another string.
1 2 3 4 5 | <?php echo strpos("I love english, I love english too!","english"); ?> |
Output:
1 2 3 | 7 |
11. preg_match
Finds the pattern in a string, returning true if the pattern exists, otherwise false.
1 2 3 4 5 6 | <?php preg_match("/^sad/", "Alex was sad"); // returns false preg_match("/^sad/", "sad Alex!"); // returns true ?> |
12. sprintf
Creates a formatted string from one or more arguments.
1 2 3 4 5 6 7 8 | <?php $str = "Welcom"; $n = 2; $formatted_str = sprintf('%s, to %uth chapter of PHP.',$str, $n); echo $formatted_str; ?> |
Output:
1 2 3 | Welcom, to 2th chapter of PHP. |
13. trim
Removes spaces and other predefined characters from both sides of a string.
1 2 3 4 5 6 7 | <?php $str = ' Welcome to code4example.com '; $res = trim($str); echo $res; ?> |
Output:
1 2 3 | Welcome to code4example.com |
14. strtolower
Converts a string to lowercase.
1 2 3 4 5 | <?php echo strtolower("Hello WORLD."); ?> |
Output:
1 2 3 | hello world. |
15. file_exists
Checks whether a file or directory exists or not.
1 2 3 4 5 | <?php echo file_exists("file.txt"); ?> |
16. is_string
Is used to determine whether a variable is a string or not.
1 2 3 4 5 6 7 8 | <?php if (is_string("0011")) echo "It's a string\n"; else echo "It is not a string"; ?> |
Output:
1 2 3 | It's a string |
17. preg_replace
To perform a pattern match on a string, then replace the match with specific text. This example removes spaces in $str.
1 2 3 4 5 6 7 | <?php $str = 'Hello sir'; $str = preg_replace('/\s\s+/', ' ', $str); echo $str; ?> |
Output:
1 2 3 | Hello sir |
18. file_get_contents
Read a file in a character string.
1 2 3 4 5 | <?php echo file_get_contents("file.txt"); ?> |
19. array_key_exists
Checks an array for a specified key and returns true if the key exists and false if the key does not exist.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $car = array("Renault"=>"Kangoo", "Peugeot"=>"Crossover"); if (array_key_exists("Renault",$car)) { echo "The key exists!"; } else { echo "The key does not exist!"; } ?> |
Output:
1 2 3 | The key exists! |
20. array_keys
Returns an array containing the keys.
1 2 3 4 5 6 | <?php $car = array("Renault"=>"Kangoo", "Peugeot"=>"Crossover", "Toyota"=>"4X4"); print_r(array_keys($car)); ?> |
Output:
1 2 3 4 5 6 7 8 | Array ( [0] => Renault [1] => Peugeot [2] => Toyota ) |
21. dirname
Returns the name of the directory for a given path.
1 2 3 4 5 | <?php echo dirname("/path/script.php"); ?> |
22. function_exists
Check if a function exists or not in the PHP script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php function displayMsg() { echo "Welcome to Code4Example"; } // Checks if the function named displayMsg exists or not if (function_exists ('displayMsg')) { echo "The displayMsg () function exists.\n"; } else { echo "The displayMsg () function does not exist.\n"; } ?> |
Output:
1 2 3 | The displayMsg () function exists. |
23. array_map
Sends each value of an array to a user-defined function and returns an array with the new values, provided by the user-defined function.
1 2 3 4 5 6 7 8 9 10 11 | <?php function multiplication($n) { return($n * $n); } $tab = array(1,2,3,4); print_r(array_map("multiplication", $tab)); ?> |
Output:
1 2 3 4 5 6 7 8 9 | Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 ) |
24. get_class
This function retrieves the class name of the given object.
1 2 3 4 5 6 7 8 | <?php // create an object $obj = new Foo(); // get the class name echo get_class($obj); ?> |
25. class_exists
This function checks if the given class has been defined.
1 2 3 4 5 6 7 | <?php if (class_exists('Foo')) { $obj = new Foo(); } ?> |
26. is_object
Is used to determine whether a variable is an object or not.
1 2 3 4 5 6 7 | <?php if (!is_object($obj)) { return false; } ?> |
27. time
Function returns the current time as seconds.
1 2 3 4 5 6 | <?php $t = time(); echo($t); ?> |
28. json_encode
Function that converts PHP objects to JSON.
1 2 3 4 5 6 7 8 9 10 | <?php $obj->nom = "Alex"; $obj->age = 24; $json = json_encode($obj); echo $json; ?> |
29. date
This function allows you to format a date.
1 2 3 4 5 6 7 | <?php echo "Today it's ". date("Y/m/d"); echo "Today it's ". date("Y.m.d"); echo "Today it's ". date("Y-m-d"); ?> |
30. is_null
Function is used to test whether a variable is NULL or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $var = TRUE; if (is_null($var)) { echo "The variable is NULL"; } else { echo "The variable is not NULL"; } ?> |
31. is_numeric
Is used to check whether a variable is numeric or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $var = 678; if (is_numeric($var)) { echo "$var is a numeric value." ; } else { echo "$var is not a numeric value." ; } ?> |
Output:
1 2 3 | 678 is a numeric value. |
32. array_shift
Removes the first element from an array and returns the value of the deleted element.
1 2 3 4 5 6 7 | <?php $colors = array(0=>"orange", 1=>"green", 2=>"blue"); echo array_shift($colors); print_r ($colors); ?> |
33. defined
Checks if a constant exists.
1 2 3 4 5 6 | <?php define("PI", 3.14); echo defined("PI"); ?> |
34. is_dir
Checks whether the specified file is a directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $directory = "documents"; if(is_dir($directory)) { echo ("$directory is a directory"); } else { echo ("$directory is not a directory"); } ?> |
35. json_decode
Decode a JSON string
1 2 3 4 5 6 | <?php $json = '{"a":1,"b":2,"c":3}'; var_dump(json_decode($json)); ?> |
36. header
Sends a raw HTTP header to a client.
1 2 3 4 5 6 7 | <?php header("Expires: Tur, 11 May 2022 01:00:00 GMT"); header("Pragma: no-cache"); header("Cache-Control: no-cache"); ?> |
37. strtoupper
Converts a string to uppercase.
1 2 3 4 5 | <?php echo strtoupper("Hello WORLD!"); ?> |
38. array_values
Returns an array containing all the values of an array.
1 2 3 4 5 6 | <?php $info = array("Name"=>"Peter", "Age"=>"26", "City"=>"L.A."); print_r(array_values($info)); ?> |
Output:
1 2 3 4 5 6 7 8 | Array ( [0] => Peter [1] => 26 [2] => L.A. ) |
39. md5
Calculates the MD5 hash of a string.
1 2 3 4 5 6 | <?php $str = "Code4Example"; echo md5($str); ?> |
40. method_exists
It checks if the “write” method exists in the object supplied as the “$book” parameter.
1 2 3 4 5 6 | <?php $book = new Book(); var_dump(method_exists($book, 'write')); ?> |
41. file_put_contents
Write the string of characters “Welcom to Code4Exampe! “In the file” file.txt.
1 2 3 4 5 | <?php echo file_put_contents("file.txt","Welcom to Code4Example!"); ?> |
42. rtrim
Removes spaces or other predefined characters to the right of a string.
1 2 3 4 5 6 | <?php $var = "Hello Code4Example!"; echo rtrim($var, "Code4Example!"); ?> |
43. array_pop
Remove the last element from an array.
1 2 3 4 5 6 7 | <?php $colors = array("orange","yellow","blue"); array_pop($colors); print_r($colors); ?> |
44. unlink
Delete a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $file = "file.txt"; if (!unlink($file)) { echo ("Error during deletion"); } else { echo ("File deleted"); } ?> |
45. basename
Returns the file name from a path.
1 2 3 4 5 6 | <?php $path = "/prog/app.php"; echo basename($path); ?> |
46. realpath
Returns the absolute path.
1 2 3 4 5 | <?php echo realpath("myfile.txt"); ?> |
47. call_user_func
Is used to call a callback function given in the first parameter and passes the remaining parameters as arguments. It is used to call user-defined functions.
1 2 3 4 5 6 7 8 9 10 | <?php function display($val) { echo "This is the $value site.\n"; } call_user_func('display', "Code4Example.com"); ?> |
48. call_user_func_array
Calls a callback function and passes the parameters to it. The parameters are encapsulated in an array and then assigned one by one to the callback function parameter list. You call the callback function by the fully qualified name assigned to it in the first parameter. If the callback function returns a value, that value is returned by call_user_func_array.
1 2 3 4 5 6 7 8 9 | <?php $mult = function($nbr1, $nbr2) { return $nbr1 * $nbr2; }; var_dump(call_user_func_array($mult, array(3, 2))); ?> |
49. fopen
Open a file or URL.
1 2 3 4 5 6 | <?php $file = fopen("myFile.txt","r"); $url = fopen("http://code4example.com/","r"); ?> |
50. microtime
Returns the current Unix timestamp in microseconds.
1 2 3 4 5 | <?php echo(microtime()); ?> |
The 100 PHP Functions You Should Know – Part 2
[…] The 100 PHP Functions You Should Know – Part 1 […]