EDIT: Disregard this, invalid test. Iterating 1M times instead of 1k irons out the wrinkles.

Ever wondered whether it's faster to directly cast a variable versus invoke a casting function in PHP?

I wondered, so I wrote a script that calls (bool) $int 1000 times, then invokes boolval($int) 1000 times. It turns out that boolval() is between 15% and 250% faster than an explicit cast.

I'll bet the same is true for intval(), floatval(), and strval().

#PHP

@ossobuffo You most certainly did something incorrectly when testing. Assuming `boolval()` is using a fully-qualified name (when in a namespace) both should have the same performance, since PHP will optimize `boolval()` into `(bool)`. If not, the explicit cast should be faster.
@ossobuffo Please see https://tideways.com/profiler/blog/how-we-use-hyperfine-to-measure-php-engine-performance from @edorian for insight on how to correctly (micro)benchmark PHP.
How we use hyperfine to measure PHP Engine performance

One of our recurring jobs at Tideways is to ensure that all of our instrumentation works with new and upcoming PHP versions. For us, "working" doesn’t just mean that the results are correct, but that your PHP extension is fast, gathering insights for our customers with a minimal performance overhead. While we have a comprehensive automated test suite, especially for new [...]

Tideways