Several PHP code writing and skills sharing that can improve the running efficiency, and php writing
Look at the sample code.
I. Traverse Arrays
Pay attention to the count usage in the traversal array. Do not calculate the array length every time.
Slow writing
Copy codeThe Code is as follows:
<? Php
$ Array = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,....);
For ($ I = 0; $ k <count ($ array); $ I ++ ){
Echo $ array [$ I];
}
?>
Efficient writing
Copy codeThe Code is as follows:
<? Php
$ Array = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,....);
For ($ I = 0, $ k <count ($ array); $ I <$ k; $ I ++ ){
Echo $ array [$ I];
}
?>
Ii. Clever Use of functions
Select the applicable function as needed. If you know the time of a date type '2017-06-04 10:43:00 ', you only need to obtain the year, month, and day.
Slow writing
Copy codeThe Code is as follows:
<? Php
$ Date = '2017-06-04 10:43:00 ';
$ Arr = explode ('', $ date );
Echo $ arr [0];
?>
Efficient writing
Copy codeThe Code is as follows:
<? Php
$ Date = '2017-06-04 10:43:00 ';
Echo substr ($ date, 0, 10 );
?>
3. Single double quotation marks
Many people mistakenly believe that single quotes are used the same as double quotes, which is a serious mistake. In PHP, single quotes and double quotes are very different. The biggest difference is that variables can be parsed in double quotes, but not in single quotes. This leads to efficiency problems. Single quotes are more efficient than double quotes.
Slow writing
Copy codeThe Code is as follows:
<? Php
// Low Efficiency
$ Str = "a variable value ";
Echo "this is a double quotation mark string {$ str }";
?>
Efficient writing
Copy codeThe Code is as follows:
<? Php
// Low Efficiency
$ Str = 'variable value ';
Echo 'this is a double quotation mark string '. $ str;
?>
Iv. Try to be concise
View Code directly
Normal writing
Copy codeThe Code is as follows:
<? Php
Function cheng ($ a, $ B ){
$ C = $ a * $ B;
Return $ c;
}
$ Result = cheng (10, 16 );
Echo $ result;
?>
Concise writing
Copy codeThe Code is as follows:
<? Php
Function cheng ($ a, $ B ){
Return $ a * $ B;
}
Echo cheng (10, 16 );
?>
5. Use of branches
If there are too many branches, use switch. If there are few, use ifelse
Slow writing
Copy codeThe Code is as follows:
<? Php
If ($ a = 1 ){
// Code block
} Elseif ($ a = 2 ){
// Code block
} Elseif ($ a = 3 ){
// Code block
} Elseif ($ a = 4 ){
// Code block
} Elseif ($ a = 5 ){
// Code block
}...
?>
Efficient writing
Copy codeThe Code is as follows:
<? Php
Switch ($ ){
Case 1:
// Code block 1
Break;
Case 2:
// Code block 2
Break;
Case 3:
// Code block 3
Break;
...
Default:
// Default Block
}
?>
As a server-side language, PHP is particularly important in programming. maintaining an efficient style will make your program run more smoothly!
How does php reduce server consumption and improve efficiency?
There are many ways to optimize PHP. You can work hard on servers, server software such as APACHE, databases such as MYSQL, but the most important thing is to work hard on PHP code, faster algorithms and less computing.
The following is an excerpt:
1. If a method can be static, it will be declared as static. The speed can be increased to 4 times.
2. echo is faster than print.
3. Use multiple echo parameters instead of periods to replace string connections.
4. determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value for each loop.
5. Cancel unnecessary variables, especially large arrays, to release the memory.
6. Avoid using _ get ,__ set ,__ autoload whenever possible.
7. require_once () is expensive.
8. If you use the full path when the file is included, 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 (the SERVER receives the 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 arrays as 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.
15. Open the mod_deflate module of apache.
16. The database connection should be closed after use.
17. $ row ['id'] is 7 times the efficiency of $ row [id.
18. The error message is expensive.
19. do not use functions in a for loop, such as for ($ x = 0; $ x <count ($ array); $ x). The count () function is called once every loop.
20. Increase the local variable in the method at the fastest speed. It is almost the same as calling a local variable in a function.
21. Increasing a global variable is twice slower than increasing a local variable.
22. Incrementing an object property (for example, $ this-> prop ++) is three times slower than incrementing a local variable.
23. Increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.
24. Defining only one local variable without calling it in the function also slows down (to an extent equivalent to increasing a local variable ). PHP will probably check whether global variables exist.
25. 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.
26. The methods in the derived class run faster than the same methods defined in the base class.
27. Calling an empty function with a parameter takes seven to eight times to increment local variables. Similar method calls take nearly 15 times to increment local variables.
28. Use single quotes instead of double quotes to include strings, which is faster. Because PHP searches for variables in strings enclosed by double quotation marks, but not in single quotes. Of course, this can be done only when you do not need to include a variable in a string.
29. When multiple strings are output, use commas instead of periods to separate strings, which is faster. Note: Only echo can do this. It is a "function" that can treat multiple strings as parameters, so the function is added with double quotation marks ).
30. 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.
31. Unless the script can be cached, it will be re-compiled every time it is called. Introduce a PHP program... the remaining full text>
How to improve the efficiency of reading large files in php
It mainly depends on how you use it. If it is read only and then written to another file, the speed will not be too slow.
Let's show you the demo address:
Service.020i.cn/baidu/bigfile.php