Analysis of 50 tips to improve the execution efficiency of PHP website programs

黄舟
Release: 2023-03-07 06:40:02
Original
1080 people have browsed it

PHP is a general-purpose open source scripting language. The syntax absorbs the characteristics of C language, Java and Perl. It is easy to learn and widely used. It is mainly suitable for the field of Web development. Hangzhou Danai PHP training experts have collected and compiled 50 tips to improve the execution efficiency of PHP website programs:


1. Use single quotes instead of double quotes to contain the string. , this will be faster.


#2. Try to define the class method as static, the speed will be increased by nearly 4 times.


3. $row['id'] is 7 times faster than $row.


4. echo is faster than print, and uses multiple parameters of echo instead of string concatenation.


#5. Determine the maximum number of loops before executing the for loop. It is best to use foreach instead.


6. Unregister unused variables, especially large arrays, to free up memory.


7. Try to avoid using get, set, and autoload.


8, require_once() is expensive.


9. Try to use absolute paths when include files to avoid PHP searching for files in include_path. The time required to parse the operating system path will increase. less.


10. If you want to know the time when the script starts executing, use $_SERVER['REQUEST_TIME'] is better than time()


11, Function replaces regular expressions to complete the same function.


12. The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.


13. If a string replacement function accepts arrays or characters as parameters, and the parameter length is not too long, then you can consider writing an additional replacement code so that each Instead of just writing one line of code to pass the parameter as a character, accept an array as query and replace parameters.


14. It is better to use a switch case than to use multiple if and else if statements.


#15. Using @ to block error messages is very inefficient, extremely inefficient.


16. Open apache's mod_deflate module to increase the browsing speed of web pages.


17. The database connection should be closed when finished using it. Do not use long connections.


18. Error messages are expensive.


#19. Increasing local variables in a method is the fastest. Close to the speed of calling local variables in a function.


#20. Incrementing a global variable is 2 times slower than incrementing a local variable.


21. Incrementing an object attribute is 3 times slower than incrementing a local variable.


#22. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.


23. Just defining a local variable without calling it in the function will also slow down the speed. PHP will probably check to see if a global variable exists.


#24. Method invocation seems to have nothing to do with the number of methods defined in the class.


#25. Methods in derived classes run faster than the same methods defined in base classes.


#26. Calling an empty function with one parameter takes the same time as performing 7 to 8 local variable increment operations. A similar method call takes close to 15 local variable increment operations.


27. The time it takes for Apache to parse a PHP script is 2 to 10 times slower than parsing a staticHTML page. Try to use more static HTML pages and less scripts.


28. Unless the script can be cached, it will be recompiled every time it is called. Introducing a PHP caching mechanism can usually improve performance by 25% to 100% to eliminate compilation overhead.


29. Try to cache as much as possible, you can use memcached. Memcached is a high-performance memory object caching system that can be used to accelerate dynamic web applications and reduce database load. Caching of OP codes is useful so that scripts do not have to be recompiled for each request.


30,

Since strlen() is a function, it will be a bit slow, because the function call will go through many steps, such as lowercase letters (referring to the lowercase function name, PHP does not distinguish between upper and lower case of function names), hash search will be executed together with the called function. In some cases, you can use the isset() trick to speed up the execution of your code.


Calling isset() happens to be faster than strlen(), because unlike the latter, isset(), as a language construct, means that its Function lookup and letter lowercase are not required for execution.


31. When executing the increment or decrement of variable $i, $i++ will be slower than ++$i. This difference is specific to PHP and does not apply to other languages. ++$i is faster because it only requires 3 instructions (opcodes), while $i++ requires 4 instructions. Post-increment actually creates a temporary variable that is subsequently incremented. Prefix increment increases directly on the original value. This is a type of optimization processing.


32. It is not necessarily Object-oriented(OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.


#33. It is not necessary to use classes to implement all data structures. Arrays are also very useful.


34. Don’t subdivide the methods too much. Think carefully about which codes you really plan to reuse?


35. When you need it, you can always decompose the code into methods.


#36. Try to use a large number of PHP built-in functions.


#37. If there are a large number of time-consuming functions in the code, you can consider using C extensions to implement them.


#38. Profile your code. The checker will tell you which parts of the code take how much time. The Xdebug debugger includes inspection routines that evaluate the overall integrity of your code and reveal bottlenecks in your code.


39. mod_zip can be used as an Apache module to instantly compress your data and reduce data transmission volume by 80%.


40. When file_get_contents can be used instead of file, fopen, feof, fgets and other series of methods, try to use file_get_contents, which will be more efficient


41. Perform as few file operations as possible. Although PHP’s file operation efficiency is not low


42. Optimize the Select SQL statement. Where possible, perform as few Insert and Update operations as possible


43. Use PHP internal functions as much as possible


44. Do not declare variables inside the loop, especially large variables: objects


45. Multidimensional arrayTry not to loop nested assignments;


46. You can use PHP In the case of internal string manipulation functions, do not use regular expressions;


47. foreach is more efficient, try to use foreach instead of while and for loops;


48. Use single quotes instead of double quotes to quote strings;


49. "Use i+=1 instead of i=i+1. It conforms to the habits of c/c++ and is more efficient.";


50. For global variables, You should unset() it after use;

The above is the detailed content of Analysis of 50 tips to improve the execution efficiency of PHP website programs. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!