In this tutorial we will discover the list of the 100 most used native functions in PHP.
51. fclose
Close an open file. This function returns TRUE on success or FALSE on failure.
1 2 3 4 5 6 7 |
<?php $file = fopen("file.txt","r"); //... fclose($file); ?> |
52. is_int
Is used to test whether the type of the specified variable is an integer or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $nbr = 123; if (is_int($nbr)) { echo "$nbr is an integer" ; } else { echo "$nbr is not a whole number " ; } ?> |
53. is_file
Checks whether the specified file is a regular file. This function returns TRUE if it is a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $file = "file.txt"; if(is_file($file)) { echo ("$file is a regular file"); } else { echo ("$file is not a regular file"); } ?> |
54. array_slice
Returns the selected parts of an array.
1 2 3 4 5 6 |
<?php $colors = array("orange","black","white","yellow","green"); print_r(array_slice($colors, 2)); ?> |
55. preg_match_all
Match all occurrences of the pattern in the string.
1 2 3 4 5 6 7 |
<?php preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 222-1414 or 1-900-888-1414", $phones); print_r($phones); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Array ( [0] => Array ( [0] => 222-1414 [1] => 900-888-1414 ) [1] => Array ( [0] => [1] => 900 ) ) |
56. ucfirst
Converts the first character of a string to uppercase.
1 2 3 4 5 |
<?php echo ucfirst("hello world!"); ?> |
57. intval
Is used to retrieve the entire part of a value.
1 2 3 4 5 |
<?php echo intval(255.22); ?> |
58. str_repeat
Repeats a string according to the number provided in parameter.
1 2 3 4 5 |
<?php echo str_repeat(".",12); ?> |
59. serialize
Converts a storable representation of a value.
1 2 3 4 5 6 |
<?php $serializedData = serialize(array('Blue', 'Red', 'Green')); echo $serializedData; ?> |
60. array_filter
filters the values of an array using a callback function.
1 2 3 4 5 6 7 8 9 10 11 |
<?php function checkOdd($nbr) { return($nbr & 1); } $tab = array("x","y",1,2,3); print_r(array_filter($tab, "checkOdd")); ?> |
61. mkdir
Create a new directory.
1 2 3 4 5 |
<?php mkdir("Document"); ?> |
62. is_callable
Is used to test only the contents of a variable, whether it can be called as a function or not.
1 2 3 4 5 6 7 |
<?php function f(){ } echo is_callable('f'); ?> |
63. ltrim
Removes spaces or other predefined characters to the left of a string.
1 2 3 4 5 6 |
<?php $var = "Hello Code4Example!"; echo ltrim($var,"Hello"); ?> |
64. ob_start
PHP being an interpreted language, each statement is executed one after the other. PHP therefore tends to send HTML code to browsers in chunks, which reduces performance. Using buffering, the generated HTML code is stored in a buffer or variable of type string and is sent to the buffer for rendering after the execution of the last statement of the PHP script.
But buffering is not enabled by default. To activate it, you must first use the ob_start() function in the script.
1 2 3 4 5 6 7 8 9 10 11 |
<?php function callback($buffer){ ..... } ob_start("callback"); echo "Hello Code4Example!"; ob_end_flush(); ?> |
65. round
Round off a floating point number
1 2 3 4 5 |
<?php echo(round(3.9754341,2)); ?> |
66. fwrite
Writes to an open file. This function returns the number of bytes written.
1 2 3 4 5 6 7 |
<?php $file = fopen("file.txt","w"); echo fwrite($file,"Hello World!"); fclose($file); ?> |
67. array_unique
Removes duplicates from a table. If two or more values ​​are the same, the first will be kept and the other will be deleted.
1 2 3 4 5 6 |
<?php $colors = array("1"=>"blue", "2"=>"orange", "3"=>"blue"); print_r(array_unique($colors)); ?> |
68. array_search
Finds a value in an array and returns the key.
1 2 3 4 5 6 |
<?php $colors = array("1"=>"blue", "2"=>"orange", "3"=>"red"); print_r(array_search("blue", $colors)); ?> |
69. reset
Moves the internal pointer to the first element of the array.
1 2 3 4 5 6 7 8 9 |
<?php $colors = array("Red", "Blue", "Green", "Orange"); echo current($colors); //Red echo next($colors); //Blue echo reset($colors); //Red ?> |
70. array_unshift
Inserts new elements into an array. The new values ​​will be inserted at the beginning of the table.
1 2 3 4 5 6 7 |
<?php $colors = array("1"=>"Red", "2"=>"Blue"); array_unshift($colors, "Orange"); print_r($colors); ?> |
71. parse_url
Processes a URL and reviews its components
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $url = 'https://username:password@host:1080/path?arg=age#12'; var_dump(parse_url($url)); var_dump(parse_url($url, PHP_URL_SCHEME)); var_dump(parse_url($url, PHP_URL_USER)); var_dump(parse_url($url, PHP_URL_PASS)); var_dump(parse_url($url, PHP_URL_HOST)); var_dump(parse_url($url, PHP_URL_PORT)); var_dump(parse_url($url, PHP_URL_PATH)); var_dump(parse_url($url, PHP_URL_QUERY)); var_dump(parse_url($url, PHP_URL_FRAGMENT)); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
array(8) { ["scheme"]=> string(5) "https" ["host"]=> string(4) "host" ["port"]=> int(1080) ["user"]=> string(8) "username" ["pass"]=> string(8) "password" ["path"]=> string(5) "/path" ["query"]=> string(7) "arg=age" ["fragment"]=> string(2) "12" } string(5) "https" string(8) "username" string(8) "password" string(4) "host" int(1080) string(5) "/path" string(7) "arg=age" string(2) "12" |
72. func_get_args
Returns the arguments of a function in the form of an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function f() { $n = func_num_args(); echo "Number of arguments : $n"; if ($n >= 2) { echo "The third argument is : " . func_get_arg(2); } } f(1, 2, 3, 4); ?> |
73. end
Moves the internal pointer to the end of the array.
1 2 3 4 5 6 7 8 |
<?php $colors = array("Red", "Blue", "Green", "Brown"); echo current($colors); echo end($colors); ?> |
74. base64_encode
Encodes the data supplied with base64. This encoding is designed to ensure that binary data survives transport through unclean transport layers.
1 2 3 4 5 6 |
<?php $str = 'This is an encoded string'; echo base64_encode($str); ?> |
75. unserialize
Converts serialized data to real data.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $serializedData = serialize(array('Java', 'PHP', 'Python')); echo $serializedData; // deserialize data $tab = unserialize($serializedData); // Display unserialized data; var_dump ($tab); ?> |
76. max
Return the largest value in an array.
1 2 3 4 5 |
<?php echo(max(3,4,6,7,20)); ?> |
77. preg_split
Converts a string to an array according to the regular expression supplied as a parameter.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $ip = "193.354.123.000"; // address IP $iptab = preg_split("/\./", $ip); print "$iptab[0]"; //193 print "$iptab[1]"; //354 print "$iptab[2]"; //123 print "$iptab[3]"; //000 ?> |
78. gettype
Is used to retrieve the type of a variable.
1 2 3 4 5 6 7 |
<?php echo gettype(55); echo gettype(false); echo gettype(array()); ?> |
79. strrpos
Finds the position of the last occurrence of a string within another string.
1 2 3 4 5 |
<?php echo strrpos("I love tomatos, I love tomatos too!","tomatos"); ?> |
80. version_compare
Compare the two standardized versions of PHP.
1 2 3 4 5 6 7 |
<?php if (version_compare(PHP_VERSION, '5.0.0', '>=')) { echo "My version is : " . PHP_VERSION . "n"; } ?> |
81. array_push
Inserts one or more elements at the end of an array.
1 2 3 4 5 6 7 |
<?php $colors = array("1"=>"orange","2"=>"brown"); array_push($colors,"red","green"); print_r($colors); ?> |
82. floor
Rounds a number to the nearest whole number, if necessary.
1 2 3 4 5 |
<?php echo(floor(0.90)); ?> |
83. strtotime
Converts a textual date-time to English in a Unix timestamp.
1 2 3 4 5 |
<?php echo(strtotime("2 May 2006")); ?> |
84. htmlspecialchars
Converts some predefined characters to HTML entities.
1 2 3 4 5 6 |
<?php $str = "Welcom <b>to</b> Code4Example.com"; echo htmlspecialchars($str); ?> |
85. ini_get
The configuration settings are in the php.ini file. The defined value of each parameter can be identified using the ini_get function.
1 2 3 4 5 |
<?php echo ini_get('upload_max_filesize'); //la sortie est par défaut 2M ?> |
86. ini_set
The configuration settings are in the php.ini file. The defined value of each parameter can be modified using the ini_set function.
1 2 3 4 5 |
<?php ini_set('upload_max_filesize', '3M'); ?> |
87. extension_loaded
This function returns TRUE if the extension identified by its name is loaded, otherwise returns FALSE.
1 2 3 4 5 6 |
<?php $str = chr(046); echo("We'll be there, you $str me!"); ?> |
88. is_bool
This function is used to determine whether a variable is a Boolean or not.
1 2 3 4 5 6 7 8 9 |
<?php $var = true; if (is_bool($var)) echo "This is a boolean"; else echo "This is not a boolean"; ?> |
89. ksort
Sorts an associative array in ascending order, by key.
1 2 3 4 5 6 7 |
<?php $age=array("55"=>"Alex","37"=>"Ali","13"=>"Emily"); ksort($age); print_r($age); ?> |
90. Extension_loaded
This function returns TRUE if the extension identified by its name is loaded, otherwise returns FALSE.
1 2 3 4 5 6 7 8 |
<?php if (extension_loaded('gd')) echo "GD installed"; else echo "GD not installed"; ?> |
91. array_reverse
Returns an array in reverse order.
1 2 3 4 5 6 |
<?php $languages = array("1"=>"C++","2"=>"Java","3"=>"PHP"); print_r(array_reverse($languages)); ?> |
92. ord
Returns the ASCII value of the first character of a string.
1 2 3 4 5 |
<?php echo ord("hello"); //The ASCII value of 'h' is 104 ?> |
93. uniqid
Generate a unique identifier.
1 2 3 4 5 |
<?php echo uniqid(); ?> |
94. strtr
Replaces a substring in a string with a character string.
1 2 3 4 5 |
<?php echo strtr("Code3Example","H3","W4"); ?> |
95. array_diff
Compares the values of two (or more) arrays and returns the differences.
1 2 3 4 5 6 7 8 9 |
<?php $languages1 = array("1"=>"PHP","2"=>"Java","3"=>"C","4"=>"C++"); $languages2 = array("5"=>"Java","6"=>"C","7"=>"C++"); $res = array_diff($languages1, $languages2); print_r($res); ?> |
96. error_reporting
Specifies which errors are reported.
1 2 3 4 5 6 7 8 9 |
<?php //Turn off error reporting error_reporting(0); //Report all errors error_reporting(E_ALL); ?> |
97. ceil
Rounds a number to the nearest whole number, if necessary.
1 2 3 4 5 |
<?php echo(ceil(0.60)); ?> |
98. urlencode
This function is useful when encoding a string for use in a request part of a URL, as it is a convenient way to pass variables to the next page.
1 2 3 4 5 |
<?php echo '<a href="cgi?foo='. urlencode($foo).'">'; ?> |
99. min
Returns the smallest value in an array.
1 2 3 4 5 |
<?php echo(min(1,2,3,0,20)); ?> |
100. print_r
Is used to display the contents of a variable in a readable way.
1 2 3 4 5 6 |
<?php $lang = array("PHP"=>"1", "Java"=>"2", "Python"=>"3"); print_r($lang); ?> |
The 100 PHP Functions You Should Know – Part 1