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
- Use single quotes over double quotes.
- If a method can be static, declare it static. Speed improvement is by a factor of 4.
- If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
- It’s better to use select statements than multi if, else if, statements.
- $row[’id’] is 7 times faster than $row[id]
- 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.
- Make use of the countless predefined function
- It’s better to use a select statement then multiple if statements with multiple else statements
- Avoid using relative path.
- Unset your variables to free memory, especially large arrays.
- 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
- 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!