php code optimization

Hi friends ,
PHP is becoming very popular scripting language and you may know this from the prabhu’s article published in this post.
It is easy for anyone to program with the help of PHP,but most the people failed in the speed (ie optimazation) section .
If your site is loading slowly then you will definitely lose the traffic.
So i am giving few tips on how to improve the performance of PHP as well as how to write the code effectively.

Use caching:

Making use of a caching module, such as “Memcache”, or a templating system which supports caching, such as Smarty, can help to improve the performance of your website by caching database results and rendered pages.

Use ini_Set in all PHP files:
Since we are not going to execute script in all the pages for long time and also we wont use long query in all the pages.
So ,always use the ini_set in each and every pages.

Use output buffering:

PHP uses a memory buffer to store all of the data that your script tries to print. This buffer can make your pages seem slow, because your users have to wait for the buffer to fill up before it sends them any data. Fortunately, you can make some changes that will force PHP to flush the output buffers sooner, and more often, making your site feel faster to your users.

Don’t copy variables for no reason:

Sometimes PHP novices attempt to make their code “cleaner” by copying predefined variables to variables with shorter names before working with them. What this actually results in is doubled memory consumption, and therefore, slow scripts. In the following example, imagine if a malicious user had inserted 512KB worth of characters into a textarea field. This would result in 1MB of memory being used!

$description = strip_tags($_POST[‘description’]);
echo $description;

There’s no reason to copy the variable above. You can simply do this operation inline and avoid the extra memory consumption:
echo strip_tags($_POST[‘description’]);

Avoid doing SQL queries within a loop:

A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts. In the example below, you can change the loop to build a single SQL query and insert all of your users at once.

foreach ($userList as $user) {
$query = ‘INSERT INTO users (first_name,last_name) VALUES(“‘ . $user[‘first_name’] . ‘”, “‘ . $user[‘last_name’] . ‘”)’;

INSERT INTO users (first_name,last_name) VALUES(“John”, “Doe”)

Instead of using a loop, you can combine the data into a single database query.
$userData = array();
foreach ($userList as $user) {
$userData[] = ‘(“‘ . $user[‘first_name’] . ‘”, “‘ . $user[‘last_name’] . ‘”)’;
$query = ‘INSERT INTO users (first_name,last_name) VALUES’ . implode(‘,’, $userData);
INSERT INTO users (first_name,last_name) VALUES(“John”, “Doe”),(“Jane”, “Doe”)…

Use single-quotes for long strings:

The PHP engine allows both single-quotes and double-quotes for string variable encapsulation, but there are differences! Using double-quotes for strings tells the PHP engine to read the string contents and look for variables, and to replace them with their values. On long strings which contain no variables, that can result in poor performance.

$output = “This is the content for a very long article
which is a few hundred lines long
and goes on and on and on

The End”;

Changing the double-quotes to single-quotes prevents the PHP engine from parsing this string in an attempt to expand variables which, in this example, don’t exist:

$output = ‘This is the content for a very long article
which is a few hundred lines long
and goes on and on and on

The End’;

Few more tips:

->Upgrade your PHP version.

->Use “echo” is faster than “print “.

->Use “Unset” or “null” your variables to free memory, especially large arrays

->Use require() instead of require_once() .

->Use full paths in includes and requires, less time spent on resolving the OS paths.

->Use $_SERVER[‘REQUEST_TIME’] is instead of time()

->Use of error supression (@) is very slow.

->Use of $row[‘id’] is 7 times faster than $row[id].

->Use of error messages are expensive.

->Use of count() in the for forloop is expensive(The count() function gets called each time)
->Use of “else if” statements are faster than “switch/case” statements.

->Use <?php … ?> tags and dont use short tags.

->Use of “++$i” is faster than “$i++”.

->Avoid magic like __get, __set, __autoload.

->If a method can be static, declare it static. Speed improvement is by a factor of 4.

Leave a Reply