50 Methods to Improve PHP program running efficiency, 50 PHP program running efficiency
0,Use single quotes instead of double quotes to include strings, which is faster. Because PHP will search for variables in strings enclosed by double quotes, but not in single quotes. Note: Only echo can do this, it is a "function" that can treat multiple character strings as parameters, so the function is added with double quotation marks ).
1,If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly four times.
2,$ Row ['id'] is 7 times faster than $ row [id.
3,Echo is faster than print and uses multiple echo parameters instead of periods to replace string connections, such as echo $ str1 and $ str2.
4,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.
5,Unregister unnecessary variables, especially large arrays, to release the memory.
6,Avoid using _ get ,__ set ,__ autoload whenever possible.
7,Require_once () is expensive.
8,When you include a file, try to use the absolute path as much as possible, because it avoids the speed of PHP to search for the file in include_path, and it takes less time to parse the operating system path.
9,If you want to know the TIME when the script starts to be executed (that is, when the SERVER receives a client REQUEST), use $ _ SERVER ['request _ time'] instead of time ()
10,Functions use the same functions instead of regular expressions.
11,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.
12,If a string replacement function can take an array or character as a parameter, and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passing is a character, instead of writing only one line of code to accept a number of groups as the query and replacement parameters.
13,Using the select branch statement is better than using multiple if and else if statements.
14,Blocking error messages with @ is very inefficient and inefficient.
15,Open the mod_deflate module of apache to speed up Web browsing.
16,When the database connection is used up, it should be switched off. Do not use persistent connections.
17,Error messages are expensive.
18,Increasing local variables in the method is the fastest speed. It is almost the same as calling a local variable in a function.
19,Increasing a global variable is twice slower than increasing a local variable.
20,Incrementing an object property (for example, $ this-> prop ++) is three times slower than incrementing a local variable.
21,Increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.
22,Defining only one local variable without calling it in a function also slows down (to an extent equivalent to increasing a local variable ). PHP will probably check whether global variables exist.
23,Method calling seems to have nothing to do with the number of methods defined in the class, because I have added 10 methods before and after the test method, but the performance has not changed.
24,The method in the derived class runs faster than the same method defined in the base class.
25,Calling an empty function with a parameter takes seven to eight times to increase the number of local variables. Similar method calls take nearly 15 times to increment local variables.
26,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.
27,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.
28,Use memcached as much as possible. Memcached is a high-performance memory object cache system that can accelerate dynamic Web applications and reduce database load. It is useful for the cache of OP code so that the script does not need to be re-compiled for each request.
29,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 (C's built-in data structure, used to store PHP variables. However, because strlen () is a function, it is more or less slow, because function calls take many steps, such as lowercase letters, PHP is case-insensitive.) and hash searches are executed along with the called function. In some cases, you can use the isset () technique to accelerate your code execution. (For example) if (strlen ($ foo) <5) {echo "Foo is too short" $} (compared with the following tips) if (! Isset ($ foo {5}) {echo "Foo is too short" $} call isset () happens to be faster than strlen (), because unlike the latter, as a language structure, isset () does not require function searches and lowercase letters for execution. That is to say, in fact, you do not spend too much money in the top-level code that checks the string length.
30,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. Therefore, do not modify your C or Java code and expect them to become faster and useless immediately. ++ $ 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. This is a kind of optimization, as the Zend PHP optimizer does. Keeping this optimization processing in mind is a good idea, because not all command optimizers perform the same optimization, and there are a large number of Internet service providers (ISPs) without command optimizers) and server.
31,It is not essential for Object-Oriented (OOP). Object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.
32,Instead of using classes to implement all the data structures, arrays are also useful.
33,Do not subdivide the methods too much. Think carefully about the code you actually intend to reuse?
34,When you need it, you can always break down the code into methods.
35,Use as many PHP built-in functions as possible.
36,If a large number of time-consuming functions exist in the code, you can consider using C extension to implement them.
37,Profile your code. The validator will tell you how much time the Code consumes. The Xdebug debugger contains an Inspection Program, which can display the code bottleneck in general.
38,Mod_zip can be used as an Apache module to instantly compress your data and reduce the data transmission volume by 80%.
39,When file_get_contents can be used to replace file, fopen, feof, fgets, and other methods, use file_get_contents as much as possible because of its high efficiency! Note the PHP version of file_get_contents when opening a URL file;
40,Perform as few file operations as possible, although PHP file operations are not efficient;
41,Optimize Select SQL statements and perform Insert and Update operations as little as possible (I have been maliciously approved for update );
42,Try to use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, it is a waste of time to write a custom function, experience problems !);
43,Do not declare variables inside the loop, especially big variables: Objects (this does not seem to be a concern in PHP ?);
44,Multi-dimensional arrays should not be nested repeatedly;
45,Do not use regular expressions when you can use PHP internal strings to operate functions;
46,Foreach is more efficient. Use foreach instead of the while and for loops;
47,Use single quotes instead of double quotes to reference strings;
48,"Replace I = I + 1 with I + = 1. In line with the c/c ++ habits, high efficiency ";
49,For global variables, unset () should be used up;