Optimize code in php


Posted on 2014-12-01 11:01:34

If you are a developer, it is essential for you to optimize your script early in the development process itself. Following the best practices while coding your PHP script is a good starting point to write a well optimized PHP code. This tutorial provides few tips to optimize PHP code from a my experiences with PHP. I hope you will sharing your experiences for everyone if you can do it. If you feel useful from this article, you can help me share it to everyone. 1. Single quotes is faster double quotes
echo "This is simple string";
If in your code which has not variable php, you should use single quotes
echo 'This is simple string';
2. Note use loop using count ($ array)
for ($i = 0; $i <count($array); $i++) {}

Each loop runs once count of functions

You should use same bellow code
$count =count($array);
for ($i = 0; $i < $count; $i++) {}

3. require / include faster than require_once / include_once 4. Using echo faster than print 5. Using echo the marks, marks faster.
echo 'My name is' . $myName;

You should using

echo 'My name is' , $myName;
6. You should unset variables a large array to free memory. 7. You use str_replace faster than preg_replace, but when you use strtr function which is faster than str_replace 8. You use @ to block error which will make slower than for system. 9. You use $i++ faster than ++$i

10. Use Ternary Operators

Instead of using an if/else statement altogether, consider using a ternary operator. PHP Value gives an excellent example of what a ternary operator looks like.
//PHP COde Example usage for: Ternary Operator
$todo = (empty($_POST[’todo’])) ?default: $_POST[’todo’]; 
// The above is identical to this if/else statement
if (empty($_POST[’todo’])) {
$action = ‘default’;
} else {
$action = $_POST[’todo’];
The ternary operator frees up line space and makes your code less cluttered, making it easier to scan. Take care not to use more than one ternary operator in a single statement, as PHP doesn’t always know what to do in those situations.
11. Some methods to speed up load website Use google tool for optimize  https://developers.google.com/speed/pagespeed/ Or use yahoo tool for optimize website http://developer.yahoo.com/yslow/ 12. Use isset Use isset( ) where ever possible instead of using count( ), strlen( ), sizeof( ) to check whether the value returned is greater than 0. For example, let us assume that you have a function which returns an array with values or a NULL array. Now you want to check whether the returned array is with values or not, then use the following:
  // do something here
In this case, use the above code block, instead of the following:
if(count($returnValue) > 0){
  // do something here

13. Use the Suppression Operator Correctly

The error suppression operator (or, in the PHP manual, the “error control operator“) is the @ symbol. When placed in front of an expression in PHP, it simply tells any errors that were generated from that expression to now show up. This variable is quite handy if you’re not sure of a value and don’t want the script to throw out errors when run. However, programmers often use the error suppression operator incorrectly. The @ operator is rather slow and can be costly if you need to write code with performance in mind. Michel Fortin has some excellent examples on how to sidestep the @ operator with alternative methods. Here’s an example of how he used isset to replace the error suppression operator:

if (isset($albus)) $albert = $albus; else $albert = NULL; is equivalent to:

$albert = @$albus;
But while this second form is good syntax, it runs about two times slower. A better solution is to assign the variable by reference, which will not trigger any notice, like this:
$albert =& $albus;
It’s important to note that these changes can have some accidental side effects and should be used only in performance-critical areas and places that aren’t going to be affected.

14. Use Caching Techniques

Use cache to reduce the load of database operations as well as the script compilation. We can use memcache for the reducing database load and APC for opcode caching and intermediate code optimization.

15. Close the Connection

Get into the habit to unset the variables and close database connection in your PHP code. It saves memory.