The 100 PHP functions to unlock the maximum power from the languageThe 100 PHP functions that you have to know

Here is the top 100 PHP functions : it is the list of the most often used PHP native functions.

The functions are named, and ranked from 1 to 100. The other 4500 functions are not ranked here. The frequency column represents how often this function is used across PHP code repositories : the reference corpus is a list of 2500 PHP Open Source projects (top 1000 composer, github/gitlab/gitee public repo, downloaded archives…). They were audited with Exakat static analysis engine, version 2.4.7. The average is the number of time the function is called within one project.

For example, 4 project out of 5 uses the count() function, and when used, count() is called around 61 times. It is both a popular and heavily used function. On the other hand, dirname() is popular but rarely used (56% for 13 usages)

Click on the link to go to the documentation. Some insight at the bottom of the top 100.

 

The 100 most popular PHP functions

1 file_exists 66.05% 18
2 count 63.67% 87
3 str_replace 63.58% 51
4 implode 62.72% 48
5 substr 60.30% 70
6 array_merge 60.02% 51
7 sprintf 59.89% 103
8 dirname 58.91% 17
9 explode 58.30% 37
10 in_array 58.27% 49
11 strpos 58.08% 31
12 is_array 58.05% 73
13 strlen 56.37% 45
14 array_key_exists 54.87% 88
15 array_keys 54.72% 23
16 preg_match 52.02% 40
17 file_get_contents 51.76% 11
18 trim 48.49% 37
19 is_string 48.46% 27
20 array_values 48.46% 10
21 is_file 46.90% 7
22 method_exists 46.62% 11
23 array_map 46.11% 17
24 file_put_contents 46.11% 7
25 strtolower 45.44% 26
26 function_exists 44.97% 21
27 preg_replace 44.40% 23
28 defined 43.44% 27
29 is_dir 41.70% 8
30 json_decode 41.54% 10
31 json_encode 41.16% 15
32 call_user_func 41.06% 5
33 array_filter 41.00% 10
34 strtr 40.55% 6
35 fwrite 40.08% 7
36 rtrim 39.85% 8
37 strrpos 39.54% 4
38 call_user_func_array 39.41% 4
39 filter_var 39.25% 3
40 class_exists 39.22% 17
41 header 38.71% 11
42 array_flip 38.62% 3
43 realpath 38.11% 6
44 ini_get 38.01% 6
45 get_class 36.01% 14
46 is_object 35.03% 15
47 unlink 34.96% 8
48 trigger_error 34.84% 8
49 array_shift 34.71% 8
50 is_int 33.76% 7
51 spl_autoload_register 33.57% 1
52 is_numeric 32.93% 13
53 strtoupper 32.93% 9
54 mkdir 32.93% 4
55 array_pop 32.52% 7
56 ltrim 31.95% 5
57 headers_sent 31.95% 2
58 fopen 31.88% 8
59 array_unique 31.79% 6
60 str_repeat 31.47% 9
61 basename 31.25% 6
62 array_slice 30.39% 5
63 fclose 29.91% 7
64 time 29.47% 16
65 is_callable 29.37% 5
66 preg_match_all 29.28% 4
67 is_bool 29.15% 3
68 microtime 28.74% 5
69 spl_autoload_unregister 28.71% 1
70 var_export 28.61% 4
71 array_unshift 28.58% 3
72 max 28.42% 7
73 array_search 28.29% 4
74 apcu_fetch 28.29% 0
75 extension_loaded 28.20% 4
76 getcwd 28.17% 2
77 preg_split 28.14% 4
78 reset 28.10% 6
79 end 28.04% 4
80 gettype 28.04% 4
81 stream_resolve_include_path 27.85% 0
82 md5 27.72% 6
83 getenv 27.66% 6
84 apcu_add 27.41% 0
85 ucfirst 27.34% 7
86 ksort 27.31% 3
87 date 27.18% 14
88 is_null 26.90% 27
89 parse_url 26.61% 3
90 preg_quote 26.61% 4
91 array_reverse 26.58% 3
92 array_diff 26.45% 4
93 base64_encode 26.14% 4
94 version_compare 26.04% 4
95 preg_replace_callback 25.91% 4
96 current 25.25% 3
97 round 25.25% 7
98 min 25.15% 4
99 serialize 25.02% 6

Top 100 insights

  • The most commonly used PHP functions are string functions, and then arrays, then files.
    • Math functions are probably out of the ranking, as they are mostly based on operators
    • Databases functions (pdo, mysqli*, pg_*…) are probably out of scope, as they are based on classes or components
  • No recently deprecated function is in the top 100.
  • Usage of composer may also reduce the need for some native function, by federating them in a component, and reducing their overall usage. For example, monolog may use log() and reduce its usage across the Open Source community.
  • Extension that makes it to the top 100 : filter, json, apcu.
  • json_encode() and json_decode() are now used with the same exact frequency, or almost.
  • md5 is the only widely used crypto function. hash() is the next, in 159th position (not shown). This must change!
  • Debug functions, such as print_r() or var_dump() are out of the top 100. var_export() is a more ambiguous case.
  • array, echo, print, empty, isset and other language constructs were not counted as functions in this ranking. They are probably trusting the first ranks anyway.
  • Several functions could and should be replaced by operators : is_object(), call_user_func(), call_user_func_array()
  • max() is more often than min()
  • Types are still a major concern of PHP code : is it a string? is it an object? No, it’s a resource!
    • Type system may go a long way, but there are still return values that needs testing.
  • There is also a PHP top 100 for the PHP classes. Well,

Challenges

If you are learning PHP, it is a good idea to review the 100 functions ranked here : they are the features you’ll find the most often when landing on a coding team. They are not the only ones, but you’ll be less surprised when meeting them.

More details?

Follow us on BlueSky or mastodon if you want more details, more stats