10 Simple tips to optimize and run your php code faster

PHP is one of the most popular web programming language and it is used widely in many open source and commercial scripts. The beauty of php is its flexibility, easy to learn, highly optimized and specially made for web based applications. A php script can run slower if you do not care about memory, functional and cache optimization rules. Here are 10 most simple tips to increase the performance of your php scripts.

  1. Whenever you need to print something to browser, use echo instead of print. Because echo is always faster than print command. And for echo, always use its multi parameter facility to output multiple strings or variables instead of using dot (.) or concatenate operator. For example use:
    <?php
        echo "This is a string","this is another string",$variable;
    ?>
    

    instead of

    <?php
        echo "This is a string"."this is another string".$variable;
    ?>
    
  2. Free memory as much as possible. Unset variables and specially large arrays,objects which are no longer being used in your scripts.
  3. For replacement of strings, use str_replace instead of preg_replace for simple replacement. Use preg_replace only when you are going to use regex match. And use strtr instead of str_replace whenever possible. Because strtr is 4 times faster than str_replace.
  4. When there are a lot of conditional commands, use switch statement instead of multiple if-elseif-else.
  5. Define global variables in functions only when you need to use them in those functions. Unused global variables always reduce performance. And when you are using global variables in a loop or any incremental statement, avoid it as much as possible because incrementing a local variable is faster than incrementing global variable. You can increment a temporary local variable and then push its value to global one.
  6. When using associative arrays or objects, $var['value'] is always faster than $var[value]. Use quotes whenever possible.
  7. Cache your results. Because whenever your PHP code runs, your web server have to execute script every time it runs. Caching the results will greatly improve the performance and make your scripts 25%-100% faster.
  8. isset function seems to be faster than strlen. Use following:
    <?php
        if(isset($var{4})) { echo "length is 4"; }
    ?>
    

    is better than

    
    <?php
        if(strlen($var) == 4 ) { echo "length is 4"; }
    ?>
    
  9. Always close your opened DATABASE connections when they are no longer being used.
  10. Avoid require_once and use include or require. Because require_once is always expensive.

Related posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Is this multi server? because one function works faster on linux, second faster on windows. One function works faster witch small variable but slow witch large, second works faster witch large variable… and same.

Nice, I just think point 8 is not good to remember (if(isset($var{4})), as they are going to deprecate this curly method in PHP6, use $var[4] instead

hey helpful code for people who seeking for it nice keep it up.

Common knwledge, but never the less nice post.

Without knowing I was following most of these tips, so I guess I had a good teachear :D

except closing queries and variables. Where would you unset the variable or close the queries? unset the variables at the bottom of every page and close queries once you fetched the data?

Leave a comment

(required)

(required)