PHP is undoubtedly one of the preferred development methods chosen by many to create dynamic web pages. As contents get bigger, there is a real need to manage our PHP codes so that it runs faster. Unfortunately for many beginners, designers and enthusiasts, making PHP codes run faster requires intricate knowledge of the overall infrastructure around PHP. It could be your Apache settings, your operating system, or even the memory of your machine.
But we can start from somewhere. Here are 7 areas of your own PHP code that you can modify for performance.
PHP static Keyword
Usage of the static keyword on functions is often programmer’s first choice for optimization. It allows methods within a PHP class to be accessed without the need to instantiate it.
<?php class Foo { public static function staticMethod() { // put codes here } } Foo::staticMethod(); ?>
The process of instantiating an object is always expensive and as we can see here, the function staticMethod() can be called without creating a new Foo object. This optimization could lead almost up to 4 times speed improvement.
PHP switch Statements
Most programmers prefer the general if-statements, but some performance gains can be achieved if we use the more specific switch-statements, especially when we have to deal with a lot of conditions.
<?php $value = 1; switch ($value) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?>
String Manipulation
Many casual programmers interchangeably use the single and double quotation marks in PHP. Knowing when and where to use them not only encourages proper coding technique but also faster execution in the long run. All you need to know is this, single quotation marks between a string mean that the PHP interpreter doesn’t have to scan the string for variables, as opposed to double quotation marks.
Another frequent string manipulation practice happens when we want to replace a string with another string. There are about 4 ways a programmer could achieve this, using functions sprintf, preg_replace, str_replace or strtr, albeit different low-level implementations between them. It is enough for us to know that using strtr whenever possible takes lesser time to complete than the other three, almost by a factor of 4.
Data Structures
With PHP 5 comes support for Object Oriented Programming (OOP). While OOP does promote scalability while minimising the amount of code you write, not everything has to be in OOP. Often these OOP practices, such as creating a new object or calling methods within it take a massive overhead and a lot of memory. For example, lists might be useful to store data, but arrays in PHP work the same way too. Assess how big your PHP project would be – my bet is if it is just for your personal website, you might want to stick to simpler methods.
Variables
Variables in a PHP code can be declared in many places within the entire scope, some can be a local variable contained within a method or a global variable, declared outside of the scope of methods within a PHP class. When it is not required of you to use global variables, change them as local variables as this could bring almost as much as double speed improvements.
Remember OOP? Incrementing an object property for example, is 3 times slower than a local variable.
PHP Output
Like string manipulation, there are multiple ways to output results. The general consensus point that echo is faster than print. Although the justification is pretty academic, it comes down to the design between echo and print in PHP. The former does not return a value while the latter does.
Error Handling
Although error handling could promote robustness, it is better to ensure that such erroneous outcomes never happen. Error handling, mostly used during debugging purposes, is an expensive process within PHP. For instance, avoid using the @ operator to suppress errors.