When coding in PHP in variety ways, some way is simple, some is not. But there must be one better way that can provide better performance.
Here is a list of 63+ best practice to optimize PHP code performances
I learn some from it.
- echo is faster than print.
- require_once() is expensive
- If you need to find out the time when the script started executing, $_SERVER[‘REQUEST_TIME’] is preferred to time()
- str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
- It’s better to use select statements than multi if, else if, statements.
- Close your database connections when you’re done with them
- $row[‘id’] is 7 times faster than $row[id]
- Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
- When echoing strings it’s faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
- Do NOT use SQL wildcard select. eg. SELECT *