Internal PHP function usage: revisited
Tagged with: [ PHP ]
A small update on the blogpost about PHP’s internal function usages: https://www.adayinthelifeof.nl/2014/07/25/internal-php-function-usage/
A lot of people are asking about functions like isset, empty, print, echo etc, as they are not present in the current result list. The thing is, is that these are not really functions, but language constructs. This means that PHP treats them a bit different than normal functions, and this results sometimes in seemingly “strange” behaviour when trying to use them like regular functions:
empty(trim($name))
will not work, while the following does:
myownfunction(trim($name))
In any case, I’ve revisited the results a bit, and added some of these constructs to the list:
echo
, empty
, eval
, isset
, print
, unset
and even yield
. As with the first attempt, I’ve
taken some of the peaks out of the equation: every repository with over 1000 of the same functions:
https://github.com/e107inc/e107 => define - 7000 https://github.com/eryx/php-framework-benchmark => empty - 5824 https://github.com/eryx/php-framework-benchmark => count - 5052 https://github.com/eryx/php-framework-benchmark => isset - 16320 https://github.com/eryx/php-framework-benchmark => sprintf - 5948 https://github.com/opencart/opencart => isset - 5225 https://github.com/googleads/googleads-php-lib => class_exists - 35648 https://github.com/s9y/Serendipity => define - 74004
So without these, the new results, the top 22:
86937 isset 43159 echo 31697 empty 29252 substr 26146 count 24248 is_array 22572 strlen 19365 sprintf 18090 unset 16584 str_replace 15462 strpos 15241 preg_match 14630 in_array 14305 chr 13218 trim 12747 implode 12534 explode 11853 array_merge 11850 define 11495 strtolower 10079 is_string
The rest of the results can be found here.
Some interesting facts:
isset()
is winning by a landslide. There are in the almost 1000 repositories checked, twice as much calls toisset()
than to the number two function (echo
).- After some fine-tuning and tweaking, the top-3 are all language construct, the first “real”
function
substr()
is #4, andcount()
#5. These two have swapped places from the last check because of some repositories with a unreasonable high number ofcount()
statements. echo
(#2) is used more thanprint
(#42) with a ratio of almost 5:1.- In order, the most calls are made to:
is_array
,is_string
,is_null
,is_object
,is_numeric
,is_bool
,is_callable
,is_resource
,is_scalar
,is_a
,is_integer
,is_double
,is_long
. - 529 times
eval()
is called :( - 71 times a generator is generated with
yield
.