This article is mainly to share with you the efficient PHP writing, hoping to help everyone.
1. Static statics
Static variables begin with the creation of classes, and finally the extinction of classes; non-static variables start with the creation of class instances, and finally the extinction of class instances. Static methods can be called directly, the instance method is called through the instance, the static speed will be much faster. static methods always call the same piece of memory and cannot be destroyed automatically, but instantiated can be destroyed.
2. Echo efficiency is higher than print
Because Echo does not return a value, print returns an reshape.注意,echo大字符串的时候,如果没有做调整就严重影响性能。使用打开apached的mod_deflate进行压缩或者打开ob_start先将内容放进缓冲区。
3. Set the maximum number of cycles before the loop, not in the loop;
Calculated as each time in the for loop.
4. Destroy the variable to release the memory, especially the large array;
Arrays and objects in PHP specifically account for the memory, this is due to PHP's underlying Zend engine, generally speaking, the PHP array of memory utilization is only 1/10, that is, a C language inside the 100M memory array, in PHP will be 1G. In particular, in the system of PHP as a background server, there are often problems with memory costs too much.
5. requiere_once () comparative consumption of resources;
This is because requiere_once needs to determine if the file has been referenced, so it can not be used as much as possible. Commonly used require/include methods to avoid.
6. Use absolute paths in includes and requires.
If you include a relative path, PHP will traverse the include_path to find the file. This type of problem is avoided with absolute paths, so less time is required to resolve the operating system path.
7. If you can use PHP internal string manipulation functions, try to use them, do not use regular expressions, because their efficiency is higher than the regular;
Str_replace character substitution singular replace preg_replace fast, but STRTR is 1/4 faster than str_replace;
And don't make unnecessary replacements. Str_replace also allocates memory for its parameters, even if there is no replacement. Very slow! Workaround: Use Strpos to find first (very fast), to see if replacement is needed, and then replace if necessary. Efficiency:-If you need to replace: efficiency is almost equal, the difference is about 0.1%. If you do not need to replace: Use Strpos fast 200%.
8. It is best not to use @ to cover up the error will slow down the script run;
With @ Actually there are many operations in the background. With @ than without @, efficiency gap: 3 times times. In particular, do not use @ in the loop, in the 5-cycle test, even if the first use of error_reporting (0) to turn off the error, after the cycle is completed and then open, than with @ faster.
9.$row [' ID '] is 7 times times faster than $row[id]; it is suggested to form the habit of adding quotation marks to the array keys;
in the loop, don't use the function .
For example, for ($x =0; $x < count ($array); $x), the count () function is calculated first outside;
One . Creating an undeclared local variable is 9-10 times slower than an already defined local variable
single quotes instead of double quotes to contain strings
Apache parses a PHP script 2 to 10 times times slower than parsing a static HTML page.
Use static HTML pages as much as possible and use fewer scripts.
Don't just copy the variables .
Sometimes in order to make the PHP code more neat, some novice PHP (including me) will copy the predefined variables into a shorter name of the variable, in fact, the result is an increase in memory consumption, will only make the program more slowly.
. using the Select Branch statement
The switch case is better than using multiple if,else if statements, and the code is easier to read and maintain.
As far as possible to file operations, although PHP file operation efficiency is not low;
File_get_contents can be used instead of file, fopen, feof, fgets
Use PHP intrinsics whenever possible
The foreach is more efficient and uses a foreach instead of while and for loops as much as possible .
Intra - loop do not declare variables, especially large variables: objects
for global variables, it should be unset () off;
21. Use + + $i increment
$i + + will be slower than $i + +. This difference is unique to PHP and does not apply to other languages. The post increment actually produces a temporary variable, which is then incremented. The predecessor increment is incremented directly on the original value. This is one of the most optimized processing, as Zend's PHP optimizer did.
Related recommendations:
6 Efficient PHP frameworks for creating Web applications
Learn PHP Essentials, write performance _php tutorials for efficient PHP code
Learn PHP Essentials and write automated test _php tutorials for efficient PHP code