Talking about the Crown Sports Source building the water to repair PHP code optimization Best Practices

Source: Internet
Author: User

Writing good PHP code is a critical step in creating a fast and stable web application. Following some best practice tips from the start will save you time in late pits.

  1. Use PHP's built-in approach whenever possible
    [Crown Sports Source Building bbs.yasewl.com Please add a link description as long as you can use PHP's built-in methods as much as possible, rather than writing your own method of the same functionality. Taking the time to familiarize yourself with and learn about the built-in methods of PHP not only helps you write code faster, but also allows you to write code that runs more efficiently.

  2. Using JSON instead of XML
    PHP's built-in methods, such as Json_encode () and Json_decode (), run very fast, and all should use JSON first. If you can't avoid using XML, be sure to parse it using regular expressions rather than DOM operations.

  3. Using caching technology
    Memcache is particularly useful for reducing database load, while bytecode caching engines such as APC or Opcache can save execution time when scripting is compiled.

  4. Reduce unnecessary calculations
    When a variable is used more than once, it is calculated from the beginning and is certainly more efficient than calculating it every time it is used.

  5. Use Isset () and Empty ()
    Compared to the count (), strlen (), and sizeof () functions, isset () and empty () are simpler and more efficient to detect if a variable is empty.

  6. Reduce unnecessary classes
    If you're not going to reuse a class or method, then it doesn't have the value of being there. And if you have to define and use a class, you need to plan the methods in your class reasonably, and for a method that is not particularly common, try to put them into subclasses, because calling methods in subclasses is faster than calling the parent class method.

  7. Closing related code and error reports for debugging in a production environment
    Opening error reports at development time allows you to avoid a lot of bugs, and some debugging code can help you locate bugs, but when the code is deployed to a production environment, these error reporting and debugging code slows down your program speed and presents some error reports directly to the user, as well as a considerable security risk. Therefore, in a production environment, close them.

  8. To close a database connection
    When you are finished, you can free up valuable memory resources by unregistering variables and closing database connections.

  9. Using aggregate functions to reduce database queries
    When querying a database, you can use aggregate functions to reduce the frequency of database retrieval and to make programs run faster.

  10. Use a powerful string manipulation function
    For example, Str_replace () is faster than Preg_replace () and the STRTR () function is four times times faster than the Str_replace () function.

  11. Use single quotation marks as much as possible
    If possible, use single quotes instead of double quotes. When the program runs, the variables in the double quotation marks are checked, which slows down the performance of the program.

  12. Try to use the identity operator
    because "= = =" Checks only the closed range, it is faster compared to using "= =".

Talking about the Crown Sports Source building the water to repair PHP code optimization Best Practices

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.