How to speed up your PHP scripts?

The web is an ever-changing technology landscape. In spite of threats from the newer faster nascent NodeJs, PHP is a language that is not going to disappear immediately. Over the years, PHP has become a monstrous technology.

 

Broadband connections are a norm of these days, so we do not need to worry about internet speeds as well as file size of our pages. We should go the extra mile to make sure we reduce the load time on our server and also try to decrease the number of HTTP requests.

 

Below are a few tricks that can be very helpful

  1. Use single quotes over double quotes.
  2. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  3. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
  4. It’s better to use select statements than multi if, else if, statements.
  5. $row[’id’] is 7 times faster than $row[id]
  6. Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string.
  7. Make use of the countless predefined function
  8. It’s better to use a select statement then multiple if statements with multiple else statements
  9. Avoid using relative path.
  10. Unset your variables to free memory, especially large arrays.
  11. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times
  12. Do not implement every data structure as a class, arrays are useful, too

Follow these simple steps and you can speed up your scripts in no time!