Top 100 most used PHP constants
Here is the top 100 PHP constants : it is the list of the most often used PHP native global constants.
The classes are ranked in order of project frequency. In a corpus of 3000 OSS projects, the PHP native constants were detected. They may be used directly E_ALL
or with a fully qualified name E_ALL
. Usage is measured by using a constant at least once in a project.
For example, more than 54% projects use the DIRECTORY_SEPARATOR constant. That constant is used on average 26 times in projects that use it.
Some extra insights are at the bottom of the top 100.
Rank | CONSTANT | Popularity | Average |
1 | DIRECTORY_SEPARATOR | 54.08% | 27 |
2 | PHP_EOL | 46.81% | 20 |
3 | PHP_VERSION | 36.21% | 3 |
4 | PHP_SAPI | 33.43% | 2 |
5 | E_USER_ERROR | 32.11% | 3 |
6 | E_USER_DEPRECATED | 31.45% | 4 |
7 | FILTER_VALIDATE_BOOLEAN | 31.19% | 1 |
8 | PHP_VERSION_ID | 31.19% | 4 |
9 | STDERR | 29.60% | 1 |
10 | PHP_INT_MAX | 21.01% | 3 |
11 | JSON_PRETTY_PRINT | 19.49% | 2 |
12 | PATHINFO_EXTENSION | 18.86% | 1 |
13 | STR_PAD_LEFT | 17.91% | 2 |
14 | E_USER_WARNING | 17.34% | 3 |
15 | PREG_SPLIT_NO_EMPTY | 16.95% | 1 |
16 | PHP_OS | 16.48% | 1 |
17 | PREG_SET_ORDER | 16.45% | 1 |
18 | ENT_QUOTES | 16.25% | 6 |
19 | PREG_SPLIT_DELIM_CAPTURE | 16.06% | 1 |
20 | E_ALL | 15.79% | 1 |
21 | JSON_UNESCAPED_SLASHES | 15.63% | 1 |
22 | PREG_OFFSET_CAPTURE | 15.26% | 1 |
23 | LOCK_EX | 14.17% | 1 |
24 | JSON_ERROR_NONE | 13.58% | 1 |
25 | CURLOPT_RETURNTRANSFER | 13.31% | 1 |
26 | JSON_UNESCAPED_UNICODE | 13.02% | 1 |
27 | E_WARNING | 12.78% | 1 |
28 | JSON_THROW_ON_ERROR | 12.59% | 1 |
29 | E_USER_NOTICE | 12.36% | 1 |
30 | FILE_APPEND | 12.26% | 1 |
31 | CURLOPT_URL | 11.89% | 1 |
32 | E_NOTICE | 11.76% | 1 |
33 | DEBUG_BACKTRACE_IGNORE_ARGS | 11.63% | 1 |
34 | ENT_COMPAT | 11.36% | 1 |
35 | CURLOPT_POSTFIELDS | 11.36% | 1 |
36 | FILTER_VALIDATE_URL | 11.27% | 1 |
37 | FILTER_VALIDATE_IP | 11.13% | 1 |
38 | PHP_INT_SIZE | 11.07% | 1 |
39 | FILTER_VALIDATE_EMAIL | 11.00% | 1 |
40 | E_ERROR | 10.97% | 1 |
41 | CURLOPT_HTTPHEADER | 10.80% | 1 |
42 | E_DEPRECATED | 10.70% | 1 |
43 | PHP_URL_HOST | 10.31% | 1 |
44 | SEEK_SET | 10.31% | 1 |
45 | T_WHITESPACE | 10.21% | 3 |
46 | INF | 10.18% | 2 |
47 | STDIN | 10.14% | 1 |
48 | PHP_URL_SCHEME | 10.14% | 1 |
49 | E_STRICT | 10.14% | 1 |
50 | PATHINFO_FILENAME | 10.11% | 1 |
51 | FILTER_VALIDATE_INT | 9.88% | 1 |
52 | SORT_NUMERIC | 9.84% | 1 |
53 | STR_PAD_RIGHT | 9.78% | 1 |
54 | STDOUT | 9.71% | 1 |
55 | CURLOPT_POST | 9.58% | 1 |
56 | LOCK_UN | 9.51% | 1 |
57 | SEEK_END | 9.45% | 1 |
58 | SORT_STRING | 9.42% | 1 |
59 | UPLOAD_ERR_OK | 9.15% | 1 |
60 | CURLOPT_HEADER | 9.15% | 1 |
61 | CURLOPT_SSL_VERIFYPEER | 9.08% | 1 |
62 | GLOB_NOSORT | 9.05% | 1 |
63 | PATH_SEPARATOR | 8.92% | 1 |
64 | PHP_MAJOR_VERSION | 8.92% | 1 |
65 | UPLOAD_ERR_NO_FILE | 8.85% | 1 |
66 | T_COMMENT | 8.79% | 1 |
67 | ARRAY_FILTER_USE_KEY | 8.79% | 1 |
68 | UPLOAD_ERR_INI_SIZE | 8.75% | 1 |
69 | T_DOC_COMMENT | 8.75% | 1 |
70 | GLOB_ONLYDIR | 8.69% | 1 |
71 | PHP_URL_PATH | 8.69% | 1 |
72 | UPLOAD_ERR_FORM_SIZE | 8.39% | 1 |
73 | SORT_REGULAR | 8.29% | 1 |
74 | UPLOAD_ERR_PARTIAL | 8.19% | 1 |
75 | CURLOPT_TIMEOUT | 8.19% | 1 |
76 | E_CORE_ERROR | 8.03% | 1 |
77 | PHP_QUERY_RFC3986 | 7.93% | 1 |
78 | CURLOPT_CUSTOMREQUEST | 7.93% | 1 |
79 | PHP_BINARY | 7.90% | 1 |
80 | CASE_LOWER | 7.90% | 1 |
81 | UPLOAD_ERR_CANT_WRITE | 7.90% | 1 |
82 | LC_NUMERIC | 7.83% | 1 |
83 | UPLOAD_ERR_NO_TMP_DIR | 7.83% | 1 |
84 | FILTER_FLAG_IPV4 | 7.80% | 1 |
85 | E_PARSE | 7.76% | 1 |
86 | E_COMPILE_ERROR | 7.70% | 1 |
87 | CURLOPT_FOLLOWLOCATION | 7.70% | 1 |
88 | FILTER_FLAG_IPV6 | 7.63% | 1 |
89 | SEEK_CUR | 7.63% | 1 |
90 | UPLOAD_ERR_EXTENSION | 7.57% | 1 |
91 | E_RECOVERABLE_ERROR | 7.53% | 1 |
92 | LC_ALL | 7.43% | 1 |
93 | SORT_NATURAL | 7.30% | 1 |
94 | CURLOPT_CONNECTTIMEOUT | 7.30% | 1 |
95 | T_STRING | 7.14% | 2 |
96 | PHP_INT_MIN | 7.00% | 1 |
97 | T_OPEN_TAG | 7.00% | 1 |
98 | PREG_PATTERN_ORDER | 7.00% | 1 |
99 | MB_CASE_TITLE | 6.90% | 1 |
100 | GLOB_BRACE | 6.90% | 1 |
Note: There is no direct documentation link to constants definitions and meaning, so you’ll have to search for the constants in the PHP manual.
Top 100 constants insights
Here are some insights about the list above.
- The total number of constant found is 1833. For reference, 3v4l.org‘s PHP has 800 constants. My local installation shows 3071. The total depends on the number of extensions available.
- The most commonly use PHP class is used in less than 50% of the projects. For comparison, a commonly used function is used in 62% of projects, and a class used in 46%.
- The most common constant is
DIRECTORY_SEPARATOR
, which is used to build filesystem’s paths. This shows that PHP is used to manage files in a local storage. PHP_VERSION
andPHP_SAPI
are often used, with little intensity. A lot of PHP applications apply some checks at the beginning of execution. Detecting the environment is critical to good execution.- All the PHP input and output streams are listed in the top 100, with
STDERR
the most used of all. - PHP integer limits
PHP_INT_MAX
andPHP_INT_MIN
are listed, along with theINF
constant which stands for infinity (side note : it is a float). Why do PHP code has to deal such large numbers so often? random() is only one part of the answer… - Error levels are almost all present in the top 100. Interestingly, the most often used are
E_USER_ERROR
andE_USER_DEPRECATED
: userland errors management is still a thing. - Incoming values are validated as boolean, IP, URL, email, int, IPv4, IPv6. There ar another 11 other possibilities available.
- Some popular functions have dedicated constants for configurations (flags), and they also appear here : sort(), html_entities(), preg_match(), json_encode(), fseek(), str_pad(), glob()().
- curl has a huge list of options, and manages to get 11 of them in the top 100.
- Tokens, the basic block of PHP code, managed to rank 5 constants in the top 100. There are more static analysis tools than expected.
- Lots of flags are boolean, without a named constant to designated them. It might change this top 100 if they get specific constants.
Learn the most important PHP constants
When learning PHP, it is a good idea to review the 100 constants ranked here : they are the constants you’ll find the most often when landing on a new code base. They are not the only ones, but they are the most commonly met in source.