When a language is dynamically typed, that is to say, it has loosely typed variables, it provides a higher level of abstraction that boosts the developer's productivity, but doesn't offer the best performance since its compiler has more work to do when trying to determine the datatypes of its variables. It comes as no surprise that strongly typed languages have always had better performance at runtime than loosely typed ones. This conclusion was confirmed by Facebook's HipHop project, which conducted benchmark tests with different languages and came to the conclusion that statically compiled languages always execute more quickly and consume less memory than dynamic ones.
Although PHP 7 is still a loosely typed language, it now offers the possibility to strict type variables and function signatures. This can be easily tested by executing the following code example. Let's run the following code to see its current performance:
If we execute it, we will immediately see the difference in performance:
The profiling report when strict typing variables and function signatures
The performance boost can also be seen using the microtime() function. Let's run both versions of our script and see the result:
Comparing script performance with the microtime() function
In order to fully benefit from PHP's new AST and SSA features, developers should try to strictly type variables and function signatures as much as possible. This will become especially true when the Zend Engine gets, in future releases, a Just-In-Time (JIT) compiler as this will allow for further optimizations solely based on type inference.
Also, an added bonus of strict typing is that it lets the compiler manage an aspect of code quality by eliminating the necessity of having unit tests that simply make sure that functions are behaving as expected when receiving unexpected inputs.