10 PHP Optimisation Techniques

Many people write php code without knowing how to optimise it properly to make it run faster. By applying some simple coding techniques your php script can run faster and save cpu workload. Also if your script is large then pages will load faster if the script is optimised.

1. Arrays are slower than normal variables. The speed difference is significant. Obviously only applicable to when you use small arrays. I calculated the array to take about twice as long.

2. Arrays are faster than objects. This is again a significant speed difference. Decide whether is is worth having objects in all cases. Consider using an array instead. The objects took about twice as long.

3. Functions outside of objects are slower than functions inside of objects. The function within the object takes roughly 50% longer.

4. Local variable inside a function are fractionally faster than global variables. Consider setting local variables to the value of the global variable if it is going to be used in a loop. The global took roughly 15% longer.

5. Multiplication is slightly faster than division. So it is better to multiply by 0.5 than divide by two. Consider pre calculating a division outside of a loop by storing 1/value and then multiplying with the new value. the division took about 10% longer.

6. Multiplication is a lot faster than the pow() function. Use $x * $x rather than pow($x, 2). the pow() function took roughly four times as long.

7. Receiving the return value from a function is slower than if you do not receive it. When your script is complete then the error checking by looking at the return from a function may not be necessary. Setting the variable took roughly 18% longer.

8. Using a loop is faster than using a recursive function. See if it is possible to code with a loop.

9. echo() is faster than print(). This is due to generally unnecessary error checking in print().

10. Single quotes are faster than double quotes. This is because with double quotes the text inside the quotes is checked for variables and other things. There are times to use each one but I will not go into it in depth here. Basically use single quotes when the text is completely static.

11. (Contributed by InSp3KtaH) You should not change the type of a variable. It you set $foo = 1 then you should keep it as a number. It takes time and memory to change the type.

You may have noticed that some of these techniques could come into conflict with keeping well written object orientated code. In each case you have to consider the pros and cons. There are no fixed rules so you will have to decide whether to sacrifice feed for code clarity. If large loops are to be found in your code look at optimising these very carefully as that is where a lot of performance goes.

3 Responses to “10 PHP Optimisation Techniques”

  1. InSp3KtaH Says:

    Dude most of your article is wrong .. you compare apples and oranges … Arrays and Objects are 2 different things… they dont serve the same purpose… and arrays are way faster than a bunch of variables to do the same work … you should point out that type casting is faster.. in php you cant type cast but you can code as if … like if $var = 1 … never change $var to a string .. this takes memory to do it…

    i could go on and on about php optimization .. but this article is about 75% wrong

  2. admin Says:

    I realise that arrays and objects are quite different things but for some purposes they could both be used. For example I could create an object with several variables in it to allow me to conveniently group them, like what you would use a struct in C/C++ for. In this instance it would be faster to use an array with the same functionality.

    It does take longer to access an element of an array if you run a simple test script. Like I said in the article this is only practical for very small arrays and would only be used inside a loop where the speed difference is significant. This can easily be tested if you wish to test it.

  3. Dmorph Says:

    Nice article. Another useful tool is GZIP. GZIP is a very effective way of keeping your bandwidth down when having large HTML/PHP pages.

    It compresses the page thats being sent to the user viewing your site, when the user recieves this compressed version of the page it uncompresses the page saving bandwidth and speeds up the transfer from server to browser.

    Paste ( php_value output_handler ob_gzhandler) into your .htaccess file and it will enable GZIP across all pages in that directory.

Leave a Reply