9 Tips improving php performance

It is essential for you to optimize your script early in the development process itself. As a developer, you have to do that while develop the big framework or application. The performance of PHP scripts can be optimized and help to match Google’s attention to speed.

This article provides few common tips to optimize PHP code from a developer point of view.


Do not copy of your variables to variables

Sometimes, we think we can copy predefined variables to other ones with shorter names for more readable code. That is a good idea, but if the variable is altered in any way, the memory consumption is doubled, resulting in the slowdown of scripts.


Keep away using relative paths

For better performance, reduce the use of relative paths for file inclusion. Once include file with the relative path, the PHP will search for default include paths, then continue with current directory, and so on. In such cases, a file search will take more time. However, if you need to use relative paths, the best practice is to declare the constant ROOT_PATH which defines the root path and use it afterwards.


Keep away using period for “echo” function

When using the “echo” function with periods, PHP concatenates the strings before it outputs them, decreasing the performance. Using commas instead of periods outputs them in the declared order, with no extra processing — reducing your chances of hurting application performance.


Keep away using include_once

The structure include_once includes and checks the specified file during the script execution. The functionality is similar to include, but in the case of include_once, verification is done to check that the file has been included only once.
For this reason, include_once is more costly than include statement. Sometimes it’s necessary, but you should default to include() in most situations.


Use isset

Use isset() where ever possible instead of using count(), strlen(), sizeof() to check whether the value returned is greater than 0.

For example, let us assume that you have a function which returns an array with values or a NULL array. Now you want to check whether the returned array is with values or not, then use the following:

use the above code block, instead of the following:

Close the Connection

Get into the habit to unset the variables and close database connection in your PHP code. It saves memory.

Do the same issues with the file, CURL connection as well.


Pass Reference to Function

Pass reference to the function if it does not affect your logic. A function manipulating the reference is faster than those manipulating the value been passed as here one more copy of the value is getting created. Especially it adds overhead when the value passed by you is a big array.


Use Appropriate Str Functions

str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4.


Use ===

Use “= = =” instead of “= =”, as the former strictly checks for a closed range which makes it faster.



This post is combine from other sources:

Optimize PHP code

10 tips to speed up your PHP applications