Home  >  Article  >  Backend Development  >  Good writing standards improve PHP code execution efficiency [Part 2]

Good writing standards improve PHP code execution efficiency [Part 2]

WBOY
WBOYOriginal
2016-07-25 08:48:19816browse

Use single quotes instead of double quotes to enclose strings, it will be faster. Because PHP will search for variables in strings surrounded by double quotes, single quotes will not. Note: only echo can do this, it is a "function" that can take multiple strings as parameters (Annotation: PHP Manual It is said that echo is a language structure, not a real function, so the function is enclosed in double quotes).

28. Try to cache as much as possible, you can use memcached. memcached is a high-performance in-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.
29. When operating a string and need to check whether its length meets certain requirements, you will naturally use the strlen() function. This function executes quite quickly because it does not do any calculations and just returns the known length of the string stored in the zval structure (C's built-in data structure used to store PHP variables). However, since strlen() is a function, it will be somewhat slow, because the function call will go through many steps, such as lowercase letters (Annotation: refers to the lowercase function name, PHP does not distinguish between uppercase and lowercase 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.
(Example below)
if (strlen($foo) < 5) { echo "Foo is too short"; }
(Compare with the following technique)
if ( ! isset($foo{5) })) { echo "Foo is too short"; }
Calling isset() happens to be faster than strlen(), because unlike the latter, isset(), as a language construct, means that its execution does not require Function search and lowercase letters. That is, you don't actually spend much overhead in the top-level code checking the string length.
34. 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, so please don't modify your C or Java code and expect it to be instantly faster, it won't work. ++$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 form of optimization, as done by Zend's PHP optimizer. It's a good idea to keep this optimization in mind because not all command optimizers do the same optimizations, and there are a large number of Internet Service Providers (ISPs) and servers that don't have command optimizers installed.
35. It doesn’t have to be object-oriented (OOP). Object-oriented is often very expensive, and each method and object call consumes a lot of memory.
36. It is not necessary to use classes to implement all data structures, arrays are also very useful.
37. Don’t subdivide the methods too much. Think carefully about which codes you really plan to reuse?
38. When you need, you can always break the code into methods.
39. Try to use as many PHP built-in functions as possible.
40. If there are a large number of time-consuming functions in the code, you can consider using C extensions to implement them.
41. 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.
42. mod_zip can be used as an Apache module to instantly compress your data and reduce data transmission volume by 80%.
43. When file_get_contents can be used instead of file, fopen, feof, fgets and other series of methods, try to use file_get_contents because it is much more efficient! But pay attention to the PHP version problem of file_get_contents when opening a URL file;
44. Perform as few file operations as possible, although PHP’s file operation efficiency is not low; 45. Optimize the SELECT SQL statement, where possible Perform as few INSERT and UPDATE operations as possible (I was criticized on the update page);
46. Use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, I wasted what I could have done) The time to write a custom function is a matter of experience! );
47. Do not declare variables inside the loop, especially large variables: objects (this seems to be not just a problem in PHP?); 48. Try not to loop and nest assignments in multi-dimensional arrays;
49. Do not use regular expressions when you can use PHP’s internal string manipulation functions;
50. foreach is more efficient, try to use foreach instead of while and for loops;
51. Use single quotes instead of double quotes Quote string;
52. "Use i+=1 instead of i=i+1. It conforms to the habits of c/c++ and is more efficient";
53. For global variables, you should unset() them after use.
Receive LAMP Brothers’ original PHP tutorial CD/"Essential PHP in Details" for free. For details, please contact the official website customer service: http://www.lampbrother.net
PHPCMSSecondary development http: //yun.itxdl.cn/online/phpcms/index.php?u=5
WeChat development                                                                    
Mobile Internet Server Side Development http://yun.itxdl.cn/online/server/index.php?u=5
JavascriptCourse http:// yun.itxdl.cn/online/js/index.php?u=5
CTOTraining Camp                                                                                             5

Statement:
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
Previous article:Things to learn about PHPNext article:Things to learn about PHP