Php code optimization tips (36 php optimization tips)

Source: Internet
Author: User
Php code optimization tips (36 php optimization tips)
This article introduces some php code optimization techniques. if you need them, you can refer to them.

1. if a method can be static, it is declared as static, and the speed can be increased by 1/4; 2. echo is more efficient than print. because echo does not return a value, print returns an integer. 3. set the maximum number of cycles before the loop, not in the loop; 4. destroy variables to release memory, especially large arrays; 5. avoid using magic methods such as _ get, _ set, and _ autoload. 6. requiere_once () is resource-consuming. 7. use absolute paths in pair Des and requires, so that the analysis path takes less time; 8. if you need sexinsex to the TIME when the script is executed, $ _ SERVER ['requset _ time'] is better than time (); 9. if you can use character processing functions, try to use them because the efficiency is higher than that of regular expressions. // 10. str_replace character replacement is faster than regular expression replacement preg_replace, but strtr is 1/4 faster than str_replace; 11. if a function can accept both arrays and simple characters as parameters, such as character replacement, and the parameter list is not too long, you can use some simple replacement statements, replace only one character at a time, instead of accepting arrays as search and replacement parameters. Minor events, 1 + 1> 2; 12. masking errors with @ reduces the script running speed. 13. $ row ['id'] is 7 times faster than $ row [id]. we recommend that you use array keys and quotation marks. 14. the error message is useful. do not use functions in a loop. For example, For ($ x = 0; $ x <count ($ array); $ x), the count () function is calculated first. 16. creating a global variable is twice slower than a local variable. 17. create an object property (the variables in the class) for example ($ this-> prop ++) three times slower than the local variable; 18. creating an undeclared local variable is 9-10 times slower than an initialized local variable. 19. A global variable that has not been used by any function also degrades the performance (the same as declaring the same number of local variables). PHP may check whether the global variable exists; 20. the performance of a method has nothing to do with the number of methods defined in a class, because after I add 10 or more methods to the test class (these methods are before and after the test method) No performance difference; 21. in the subclass, the method has better performance than in the base class; 22. it takes 7-8 times to run a function that calls only one parameter and the function body is empty $ localvar ++, and a similar method (function in the class) run the $ localvar ++ operation about 15 times. 23. when the output string is used, it is faster to use commas instead of vertices. Note: this only applies to echo. This function can take some strings as parameters. 24. on the apache server, a php script page takes at least two to ten times more time than the corresponding HTML static page generation. we recommend that you use static HTML pages and a few steps. 25. unless you have installed the cache, your php script needs to be recompiled every time it is accessed. We recommend that you install a php Cache program to significantly improve the performance of 20-100% by removing repeated compilation. 26. we recommend that you use memcached, a high-performance distributed memory object cache system, to improve ** application performance and reduce the burden on the database. 27. use the ip2long () and long2ip () functions to convert the IP address to an integer type and store it in the database instead of the primary type. This can reduce storage space by almost 1/4. At the same time, it is easy to sort and quickly find the addresses; 28. checkdnsrr () is used to confirm the validity of some email addresses through the existence of domain names. This built-in function ensures that each domain name corresponds to an IP address. 29. if you are using php5 and mysql4.1 or later versions, consider using the mysql _ * improved function mysqli _ *; 30. try to like to use the ternary operator (? :); 31. before you want to completely redo your project, check if PEAR has what you need. PEAR is a huge resource library that many php developers know. 32. use highlight_file () to automatically print a copy of the page source code that is well formatted; 33. use the error_reporting (0) function to prevent potential sensitive information from being displayed to users. Ideal error reports should be completely disabled in the php. ini file. However, if you are using a shared virtual host. ini you cannot modify, so you 'd better add the error_reporting (0) function and put it in the first line of each script file (or load it using require_once) this effectively protects sensitive SQL queries and paths from being displayed when an error occurs. 34. use gzcompress () and gzuncompress () to compress () large-capacity strings (decompress) before saving (extracting) the database. This built-in function can be compressed to 90% using the gzip algorithm; 35. a function has multiple return values by referencing the parameter variable address. You can add "&" before the variable to indicate passing by address rather than by value; 36. strlen () is used because it is not very fast to call other operations such as lowercase and hash table queries. we can use isset () to implement similar functions. isset () faster than strlen (); articles you may be interested in: 40 suggestions for optimizing PHP code

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.