PHP performance tips and tricks

In this php tutorial we will learn different PHP performance tips and tricks that will enhance the performance of development using php.

1) Don't use varaibles when there is no need of them

Most of the times the php developer try to clean his/her code by assigning the values of predefined variables to the variable created by him/her of shorter name. This can also become the way of better understanding of php code but it double consume the memory without any need becuase first you will declare the variable and then you will assign the values of predefined variables to those custom variables. Let's have a look over the example to better understand what i mean.

Wrong
$comments=$_POST['comments'];
echo $comments;

Correct
echo $_POST['comments'];

In the above example if the malicious user had inserted the spam text in the comments textarea of 512KB worth of characters then it would use the memory of 1MB without any need.

2) Make habit to Use single-quotes for strings

In PHP you can use both single and double-quotes for string variables but there are some differences between both of them. Using double-quotes for strings tell the engine of php to first read the content of string and then look for variables and then replace variables with their values. So if the string contains no variable then please use single-quote instead of double-quote. Let's have a look over the example

Wrong
$name="My name is Adeel Fakhar";

Correct
$name='My name is Adeel Fakhar';

It is better to use concatenation then double-quotes for string. Let's have a look over the example

Wrong
$name='Adeel Fakhar';
$myname="My name is $name";

Correct
$name='Adeel Fakhar';
$myname='My name is'.$name.' Ok';

Make habit to use echo to print

Use echo to print the results in better readability and better performance.

Wrong
print $name;

Correct
echo $name;

I will also write a post for difference between echo and print.

Don't try to use the concatenation with echo

Most of the novice php programmers don't know that they can pass multiple variables to the echo, seprating with commas, instead of concatenating them first.

Wrong
$firstname='Adeel';
$lastname='Fakhar';

echo 'My name is'.$firstname.$lastname.' OK my dear';

Correct
$firstname='Adeel';
$lastname='Fakhar';

echo 'My name is',$firstname,$lastname,' OK my dear';

Try to use switch/case instead of if/else

If there is a single variable in your php code for comparison purpose then please try to use switch/case instead of if/else for better performance and readability. Let's have a look over the example

Wrong

if($_POST['action'] == 'sum')

{

sumFunction();
}

elseif ($_POST['action'] == 'sub')

{
subFunction ();
}

elseif ($_POST['action'] == 'mul')

{
mulFunction ();
}

else {
defaultFunction();
}

Correct
switch($_POST['action'])

{
case 'sum':
sumFunction();
break;


case 'sub':
subFunction ();
break;


case 'mul':
mulFunction ();
break;


default:
defaultFunction();
break;
}

It’s my suggestion to first understand the language, clear your logic then apply these above mentioned steps,techniques,tips and tricks, what ever you say to enhance the performance of your development. As you have seen that the tutorials I have written in my blog, I haven’t follow any of these techniques because my purpose was to solve your problem quickly rather than to teach you how to write code but in professional coding life you must follow these steps in order to enhance your development performance.

If you like this post of PHP performance tips and tricks then you must thankful to code.google.com because I have read these performance enhancement techniques there and then I decided to write a post for this on my blog.

Happy Coding….. Keep Coding


1 comments:

  • Anonymous
     

    It's simply awesome.