10 suggestions for improving PHP code performance _ PHP Tutorial

Source: Internet
Author: User
10 suggestions for improving PHP code performance. The suggestions in this article cover most PHP code performance issues. If you are doing some small websites or small projects, you have reason to ignore these suggestions, but when you cover most of the suggestions in this article PHPCode performance issues. If you are doing some small websites or small projects, you have reason to ignore these suggestions. but when you provide long-term stable services for a large number of users, you must pay attention to them. Developers must consider these suggestions from the beginning of the project, which will greatly improve the performance of your website. In PHP, there are usually many methods to achieve the same purpose, but developers usually choose the most comfortable way they are most familiar, however, note the following:

1. echo is much faster than print. Both methods print things on the page, but echo does not return any value. print returns 0 or 1 if it succeeds or fails.

2. include_once is more time-consuming than include. Because it needs to check whether the class you want to include has been included.

3. for long paragraph strings, you must use single quotation marks instead of double quotation marks. Because double quotation marks are used to search for variables in strings. For example, echo 'this is long string'. $ name is much faster than echo "This is long string $ name.

4. do not use for ($ I = 0; $ I

5. if a function can be defined as a static function, do not define it as a member function. a static function is 33% faster than a member function.

6. if you can solve the problem without using a regular expression, do not use a regular expression. Regular expressions are slower than PHP native functions. For example, replace preg_replae with str_replace.

7. try not to use relative paths to include files. Find the file in the relative path, and search for it in the current directory. In this way, searching for files is slow. It is best to first define a constant such as WEB_ROOT and then use this constant to include the file.

8. equal all = faster than equal =. And if (1 = '1') will return true, if (0 = ") will also return true, if (1 = '1') and if (0 = ") both return false when you use the full equal sign. So when you need to detect some Boolean variables in the program, it is best to use the full equal sign.

9. it is best not to use the shorthand

10. it is best not to use and rely on register_global or magic_quote. be careful when reading and setting your php. ini.

Not all optimization aspects are covered here. RockUX will talk about it in a later article. Please pay more attention.

I hope this article will help you.


PHP code performance issues. If you are doing some small websites or small projects, you have reason to ignore these suggestions, but when you are doing a lot...

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.