There are some clear coding optimizations that would reduce load on the servers, and therefore remove the need for scaling out the infrastructure. It is common practice for developer which will help out to speed up php.
- Avoid function tests in loop conditionals: – If you’re looping through an array, for example, count() it beforehand, store the value in a variable, and use that for your test. This way, you avoid needlessly firing the test function with every loop iteration.
- Favor built-in functions over custom functions: – Since PHP has to take the extra step of interpreting your custom functions, built-in functions have a performance advantage. More importantly, there are a lot of useful built-in functions that you may never learn about if you always default to writing your own.
- Use full file paths on include/require statements: – Normalizing a relative file path can be expensive; giving PHP the absolute path (or even “./file.inc”) avoids the extra step.
- Pass unchanged variables to a function by reference rather than value: – This goes hand-in-hand with the point about needlessly copying variables. Much of the time, your functions only need to use the values from their parameters without changing them. In such cases, you can safely pass those parameters by reference (e.g., function(&$parameter) rather than function($parameter)) and avoid having to make memory-intensive copies.
- Ditch double quotes for single quotes: – There’s some disagreement, but the common wisdom is that PHP has to do extra processing on a string in double quotes to see if it contains any variables. Concatenation with single quotes is marginally faster.
- Caching: – Use page caching for static data. It will help to render pages quickly. Also it reduces unnecessary request for getting data from database.
- Use echo instead of print(): – As a language construct rather than a function, echo has a slight performance advantage over print().
- Debug with error_reporting(E_ALL) : – Every warning is a performance improvement waiting to happen, but only if you can see it. Cleaning up warnings and errors beforehand can also keep you from using @ error suppression, which is expensive. Turn off error reporting when you’re going to deliver that code.
- Use mod_gzip to Compress HTML: – It tells apache to compress the HTML that you send to the person browsing your website. HTML is very compressible, and this reduces your bandwidth costs, as well as massively speeds up the speed of your website, which will help no end.
The VERY FIRST LINE of your PHP code (i.e. in your header page if you use one), you should put the following line.
If you’re already using the object buffer (that’s what “ob_” stands for, by the way), then don’t have two calls to the ob_start() function, just add ‘ob_gzhndler’ as the first argument.
If you’re using apache, you’ll need to enable the mod_gzip module, and add “zlib.output_compression = On” to your php.ini. If you’re running a shared server, annoy your host until they do this.
- Avoid needlessly copying variables: – If the variable is quite large, this could result in a lot of extra processing and memory. Use the copy you already whenever possible, even if it doesn’t look good (e.g., $_POST[‘variablename’]).
- Never User star (*) in Query: – It is bad practice to use * in query. Select only required field from table. If all fields required then use *. It will take extra processing time and memory.
- SQL is faster: – You need to remember that SQL isn’t just a data store where you insert, select, update and delete data from. It’s actually a very powerful tool for processing data. If you’re ever looking to perform processing such as math, data comparison, join’s etc… PHP will always lose in a race against SQL and you should use this to your advantage.
So when you select data from SQL think is this as close to my desired output as I can get it? If it’s not making a better SQL query, this will be faster and also cut down on the amount of PHP code required.
- Use require() instead of require_once(): – Also quite a simple one to understand but when you use something like “require_once” over the alternative “require” PHP has to work if the file has already been ‘required’ in the script. Needless to say there is more thinking and therefore more processing which will slow things down, so it might look good but think do you really need it?
It is just few points which help to speed up PHP, I will add more later. If you have any point which can help to speed up please, feel free to put in comment.