10 PHP tips for dramatically improving the performance of your site

Source: Internet
Author: User
Tags define count include php code regular expression relative

The recommendations in this article cover most of the PHP code performance issues. If you are doing small Web sites or small projects, then there is reason to ignore these suggestions, but when you provide a large number of users with long-term stable service, you must pay attention to. Developers must consider these suggestions from the outset of the project, which will greatly improve the performance of your site.

In PHP, there are usually a number of ways to achieve the same goal, but developers usually choose the most comfortable way they are most familiar with, but some things must be noted:

    1. Echo is much faster than print. Two methods will print things on the page, but Echo does not return any values, and print returns 0 or 1 when it succeeds or fails.
    2. Include_once is more time-consuming than include. Because it needs to check if the class you want to include is already included.
    3. Be sure to use single quotes instead of double quotes for long paragraph strings. Because double quotes will search for variables in the string. For example, the Echo ' is long string '. The $name is much faster than the echo "This is long string $name".
    4. Do not use a for ($i =0; $i <count ($names) in a loop, $i + +), because this will compute the length of the array at each loop, instead of using $size=count ($names), for ($i =0; $i <$ size; $i + +).
    5. If you can define a function as static, then do not define as a member function, the static function is 33% faster than the member function.
    6. If you can solve a problem without a regular expression, then don't use the regular formula. Regular expressions are slower than native PHP functions. For example, use Str_replace instead of preg_replae.
    7. Try not to use relative paths to include files. Finding files in a relative path will now look in the current directory and then look again. This is a slow way to find files. It is best to define constants such as Web_root, and then use this constant to include the file.
    8. The congruent symbol = = = is faster than the equal = =. And if (1 = = ' 1′) returns true,if (0 = ") returns True, and when you use the congruent symbol, if (1 = = ' 1′ ') and if (0===) return false. So when you need to detect some Boolean variables in the program, it is best to use the congruent symbol.
    9. It's better to use <?php, because it's more likely to go wrong when you deploy the program to another server.
    10. It is best not to use and rely on Register_global or magic_quote, and be careful when reading and setting your php.ini.

This does not cover all aspects of optimization, rockux in a later article. Please pay more attention.

Reprint Please specify:
Author: rockux–web Front end
From: 10 suggestions to improve PHP code performance



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.