dreaded "Unresponsive." So read on to learn how PHP optimization can save your website.
1) Optimize PHP String Concatenation
Here's a neat little optimization trick that can save you some time when you're working with string concatentation in your PHP scripts. The traditional, non-optimized way to do this in PHP would be
echo 'String One' . 'String Two' . 'String Three';
But here's the thing. If you use commas with the echo command, as in
echo 'String One', 'String Two', 'String Three',
the server interprets this the same as
echo 'String One'; echo 'String Two'; echo 'String Three';,
and it is MUCH FASTER! So, if you just need to put some strings together and echo them, skip the concatenation and replace all of your dots (.) with commas (,). This optimization trick won't work if you need to store the value as a local variable however, as it actually bypasses the concatenation step. Remember these four words when optimizing your PHP scripts: Reduce, Restructure, Simplify, and Bypass.
2) Optimize PHP String Searching by Avioding Regex
PHP Regex functions run terribly slow. It takes a lot of computing power to check your strings against such complicated regex patterns. That's why it's important to use functions such as strcasecmp(), which case-insensitevly matches one string to another string, or strmatch() whenever possible. Yes, there are times when you just have to use a regex function like preg_match(), and having your PHP code optimized is of no use if it doesn't run properly, but look for ways to make your PHP code work without using those expensive regex functions.
3) Set ending conditions for PHP For and While Loops before entering the loop
This is a simple optimization trick that can speed up the execution of your loops in PHP. As an example, the code below
$length = count($array);
for($i=0;$i<$length;$i++) { //execute code }
only executes the count() function once, while this code
for($i=0;$i<count($array);$i++) { //execute code }
executes the count() function everytime the loop condition is executed
4) Use full paths in PHP requires and includes to Optimize server search time
When you use relative paths in include() and require(), the server must determine what directory you are trying to tell it to look in for the file. Use absolute paths to get rid of this down-time.
5) Remember to close those PHP/MySQL Connections
Leaving a MySQL connection open when it's no longer in use or never bothering to close it at the end of the file is a bad habit to get into. Those connections can pile up and slow your PHP scripts' processing times down substantially. Optimize your connections by closing them as soon as they are no longer needed.
6) PHP Error-Processing Is Slow
Suppressing errors in PHP with the '@' symbol is a tedious task. It is much better for both optimization and good programming practices to identify those errors and whenever possible correct them or find some other way to handle them.
7) Optimize your PHP by Optimizing your MySQL Queries
For PHP pages that interact with databases, MySQL queries can be some of the toughest optimization hurdles to overcome. Avoid "SELECT *" statements by grabbing only the columns that are absolutely neccessary to your query. Also make sure to look into using indexes in your MySQL database and use the EXPLAIN statement to determine how you can make your MySQL queries more efficient. Indexes are MySQL's natural way of optimizing searching through your records.
8) The Ultimate Optimization Tip: Don't use PHP when you don't need it!
PHP pages process much, much slower than regular HTML pages. So, whenever possible, revert to plain old HTML. It may not be the most exciting thing, but visitors would rather look at your static HTML than stare at their blank browsers while your state-of-the-art PHP scripts take 3 minutes to load. Decide whether or not a particular piece of your webpage has to be dynamic, and whether or not having it be dynamic adds value to your site. If not, code that part of the page in pure HTML.