Home >Backend Development >PHP Tutorial >Several code writing methods and skill sharing in PHP that can improve operating efficiency, PHP writing methods_PHP tutorial
Without further ado, let’s just look at the code examples.
1. Traverse the array
Pay attention to the number of times count is used when traversing the array, do not calculate the array length every time
A slow way to write
2. Use functions skillfully
Select the applicable function as needed. For example, if you know a date type time '2012-06-04 10:43:00', you only need to get the year, month and day.
A slow way to write
3. Single and double quotation marks
Many people mistakenly believe that single quotes are used the same as double quotes, which is a serious mistake. There is a huge difference between single quotes and double quotes in PHP. The biggest difference is that variables can be parsed in double quotes but not in single quotes. This creates an efficiency problem. Single quotes are more efficient than double quotes
Slowly written way
4. Try to be as concise as possible
Look at the code directly
Ordinary writing
5. Magical uses of branches
If there are too many branches, use switch. If there are very few, use ifelse
A slow way to write
There are many ways to optimize PHP. You can work on servers, server software such as APACHE, and databases such as MYSQL. But the most important thing is to work on the PHP code, change faster algorithms, reduce calculations, etc.
The following is an excerpt:
1. If a method can be made static, make a static declaration for it. The speed can be increased to 4 times.
2.echo is faster than print.
3. Use multiple parameters of echo (Translation: refers to using commas instead of periods) instead of string concatenation.
4. Determine the maximum number of loops before executing the for loop. Do not calculate the maximum value every time it loops.
5. Unregister unused variables, especially large arrays, to free up memory.
6. Try to avoid using __get, __set, __autoload.
7.require_once() is expensive.
8. Use the full path when including files and it will take less time to resolve the operating system path.
9. If you want to know the time when the script starts executing (annotation: the server receives the client request), using $_SERVER[‘REQUEST_TIME’] is better than time().
10. Functions replace regular expressions to complete the same function.
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 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 parameter passed is one character instead of Just write one line of code to accept arrays as parameters for query and replace.
13. It is better to use a selective branch statement (translation: switch case) than to use multiple if, else if statements.
14. Using @ to block error messages is very inefficient.
15. Open apache’s mod_deflate module.
16. The database connection should be closed when finished using it.
17.$row[‘id’] is 7 times more efficient than $row[id].
18. Error messages are expensive.
19. Try not to use functions in for loops. For example, for ($x=0; $x < count($array); $x) will call the count() function every time it loops.
20. Increasing local variables in methods is the fastest. Almost as fast as calling local variables in a function.
21. Incrementing a global variable is 2 times slower than incrementing a local variable.
22. Incrementing an object property (eg: $this->prop++) is 3 times slower than incrementing a local variable.
23. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
24. Just defining a local variable without calling it in a function will also slow down the speed (to the same extent as incrementing a local variable). PHP will probably check to see if a global variable exists.
25. Method calls appear to be independent of the number of methods defined in the class, as I added 10 methods (both before and after testing the method) and there was no change in performance.
26. Methods in derived classes run faster than the same methods defined in base classes.
27. 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.
28. Use single quotes instead of double quotes to enclose strings, which will be faster. Because PHP will search for variables in a string surrounded by double quotes, single quotes will not. Of course, you can only do this if you don't need to include variables in the string.
29. When outputting multiple strings, use commas instead of periods to separate the strings, which is faster. Note: Only echo can do this. It is a "function" that can take multiple strings as parameters (Annotation: The PHP manual says that echo is a language structure, not a real function, so the function is enclosed in double quotes. ).
30. The time it takes for Apache to parse a PHP script is 2 to 10 times slower than parsing a static HTML page. Try to use more static HTML pages and less scripts.
31. Unless the script can be cached, it will be recompiled every time it is called. Introducing a set of PHP buffer...The rest of the full text>>
It mainly depends on how you use it. If you just read it out and then write it to another file, the speed will not be too slow.
Give me a demo address:
service.020i.cn/baidu/bigfile.php