10 tips for overstating PHP code performance

Source: Internet
Author: User
Tags php framework
Improve the performance of PHP code 10 recommendations

The recommendations in this article cover most of the PHP code performance issues. If you are doing small websites or small projects, there are reasons to ignore them, but when you provide long-term, stable services to a large number of users, you must pay attention. Developers must consider these recommendations from the beginning of the project, and it will greatly improve the performance of your site. In PHP, there are usually a lot of ways to achieve the same goal, but developers often choose the most comfortable way they are most familiar with, but some things have to be noted:

1.echo is much faster than print. Two methods will print something on the page, but Echo does not return any value, and print will return 0 or 1 if 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 quotation marks instead of double quotes for long paragraph strings. Because double quotes go to search for variables in a string. For example: Echo ' This is a long string '. $name is much faster than echo "This is long string $name".

4. Do not use for ($i =0 in the loop; $i

5. If you can define a function as static, do not define it as a member function, and the static function is 33% faster than the member function.

6. If you can solve the problem without passing the regular expression, then don't use the regular. Regular expressions are slower than PHP-native functions. For example, replace Preg_replae with Str_replace.

7. Try not to include files with relative paths. Locate the file in the relative path, find it in the current directory, and then look in turn. This is very slow to find files. It is best to define constants such as Web_root, and then use this constant to include the file.

8. The same symbol = = = = = = = Faster than equal = =. And if (1 = = ' 1′) will return true,if (0 = = ") will also return true, and when you use the congruent symbol when the IF (1 = = = ' 1′) and if (0===") will return false. So it's best to use the congruent notation when you need to detect some Boolean variables in your program.

9. It's best not to use shorthand

10. It is best not to use and rely on Register_global or magic_quote, read and set your php.ini must be careful.

This does not cover all aspects of optimization, Rockux will be described in a later article. Please pay attention.

Hope that through the introduction of this article, can bring you help.

"Editor's recommendation"

    1. Explain ten reasons to use PHP framework
    2. PHP Beginner How to learn PHP explanation
    3. How PHP framework learns the new PHP framework
    4. Explain the reasons for using the PHP framework in detail
    5. PHP Beginner's Learning array declaration

"Editor: Qihappy TEL: (010) 68476606"

  • Related Article

    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.