PHP code optimization 24 Classics

Source: Internet
Author: User
Echo is faster than print. Use multiple echo parameters to replace string connection. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value every cycle. It is best to use foreach instead. For global variables, unset () should be used up. Use single quotes instead of double quotes to include strings, which is faster. Because PHP will be enclosed in double quotes

Echo is faster than print. Use multiple echo parameters to replace string connection. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value every cycle. It is best to use foreach instead. For global variables, unset () should be used up. Use single quotes instead of double quotes to include strings, which is faster. Because PHP will be enclosed in double quotes

  1. Echo is faster than print.
  2. Use multiple echo parameters to replace string connection.
  3. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value every cycle. It is best to use foreach instead.
  4. For global variables, unset () should be used up.
  5. Use single quotes instead of double quotes to include strings, which is faster. Because PHP searches for variables in strings enclosed by double quotation marks, but not in single quotes.
  6. Functions use the same functions instead of regular expressions.
  7. When the execution variable $ I increments or decreases, $ I ++ is slower than ++ $ I. This difference is exclusive to PHP and does not apply to other languages. ++ $ I is faster because it only requires three commands (opcodes) and $ I ++ requires four commands. In fact, a temporary variable is generated in post-increment mode, which is then incremented. The pre-increment directly increases on the original value.
  8. Use the select branch Statement (switch? Case) better than using multiple if, else? If statement.
  9. Use var_dump to debug PHP code. If you are looking for php debugging technology, I must say that var_dump should be your target. This command can meet all your needs in displaying php information, most of the debugging code is related to obtaining the value in PHP.
  10. When a file is included, the full path takes less time to parse the operating system path.
  11. It is a bad practice to easily create a global value, but sometimes the actual situation does need to be done. It is a good idea to use global values for database tables or database connection information, but do not use global values frequently in your PHP code. In addition, a better way is to store your global variables in a config. php file.
  12. If you want to know the TIME when the script starts to run, use $ _ SERVER ['request _ time'] instead of time ().
  13. Open the mod_deflate module of apache.
  14. Blocking error messages with @ is very inefficient.
  15. Use as many PHP built-in functions as possible.
  16. Increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.
  17. The method in the derived class runs faster than the same method defined in the base class.
  18. Defining only one local variable without calling it in the function also slows down (to an extent equivalent to increasing one local variable)
  19. Apache parses a PHP script two to ten times slower than parsing a static HTML page. Use static HTML pages and less scripts as much as possible.
  20. As mentioned before, 99% of the most important parts of any php website may be databases. Therefore, you need to be familiar with how to use SQL correctly and learn to associate tables and more advanced database technologies.
  21. Calling an empty function with a parameter takes seven to eight times to increase the number of local variables.
  22. When operating on a string and checking whether its length meets certain requirements, you can use the strlen () function. This function is executed quite quickly because it does not perform any calculations and only returns the known String Length stored in the zval structure (the built-in data structure of C, used to store PHP variables.
  23. Not all situations require the use of object-oriented development. object-oriented development usually consumes a lot of memory for each method and object call.
  24. Unless the script can be cached, it will be re-compiled every time it is called. The introduction of a PHP cache mechanism can generally improve the performance by 25% to 100%, so as to avoid compilation overhead.

Original article address: 24 real stories about PHP code optimization. Thank you for sharing them with me.

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.