Several PHP code writing methods, skills sharing, and php writing methods can improve the running efficiency. In PHP, there are several code writing methods and tips that can improve the running efficiency. php writing is not nonsense and you can directly look at the code example. I. traverse the array. in the traversal array, pay attention to several PHP code writing methods and tips that can improve the running efficiency in count usage. php code 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
The code is as follows:
<? Php
$ Array = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,....);
For ($ I = 0; $ k Echo $ array [$ I];
}
?>
Efficient Writing
The code is as follows:
<? Php
$ Array = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,....);
For ($ I = 0, $ k 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
The code is as follows:
<? Php
$ Date = '2017-06-04 10:43:00 ';
$ Arr = explode ('', $ date );
Echo $ arr [0];
?>
Efficient Writing
The 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
The code is as follows:
<? Php
// Low efficiency
$ Str = "a variable value ";
Echo "this is a double quotation mark string {$ str }";
?>
Efficient Writing
The 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
The code is as follows:
<? Php
Function cheng ($ a, $ B ){
$ C = $ a * $ B;
Return $ c;
}
$ Result = cheng (10, 16 );
Echo $ result;
?>
Concise Writing
The 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
The 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
The 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
Let's look at the sample code. 1. traverse the array. pay attention to count usage in traversing the array...