PHP's several code-writing, skill-sharing, PHP-writing methods to improve operational efficiency
Don't say much nonsense, just look at the code example.
First, iterate over the array
Note how many counts are used in iterating through the array, and do not calculate the length of the array every time
The writing of slow efficiency
Copy the Code code as follows:
<?php
$array = Array (1,2,3,4,5,6,7,8,9,10,....);
for ($i =0; $k<>
echo $array [$i];
}
?>
A quick and efficient notation
Copy the Code code as follows:
<?php
$array = Array (1,2,3,4,5,6,7,8,9,10,....);
For ($i =0, $k<><>
echo $array [$i];
}
?>
Second, skillfully use the function
Select the applicable function as needed, such as the time of day ' 2012-06-04 10:43:00 ', which is known as a date type, which only needs to be obtained by date.
The writing of slow efficiency
Copy the Code code as follows:
<?php
$date = ' 2012-06-04 10:43:00 ';
$arr = Explode (' ', $date);
echo $arr [0];
?>
A quick and efficient notation
Copy the Code code as follows:
<?php
$date = ' 2012-06-04 10:43:00 ';
Echo substr ($date, 0,10);
?>
Three, single double quotes
Many people mistakenly believe that single quotes are used just like double quotes, which is a serious mistake. In PHP, single and double quotation marks have a great difference, the biggest difference is that the double quotation marks can parse the variable, single quotation marks cannot be. The result is an efficiency problem, with single quotes being more efficient than double quotes.
The writing of slow efficiency
Copy the Code code as follows:
<?php
Low efficiency
$STR = "A variable value";
echo "This is a double-quote string {$STR}";
?>
A quick and efficient notation
Copy the Code code as follows:
<?php
Low efficiency
$STR = ' a variable value ';
Echo ' This is a double-quote string '. $str;
?>
Four, as concise as possible
Look directly at the code
General wording
Copy the Code code as follows:
<?php
Function Cheng ($a, $b) {
$c = $a * $b;
return $c;
}
$result = Cheng (10,16);
echo $result;
?>
Concise notation
Copy the Code code as follows:
<?php
Function Cheng ($a, $b) {
return $a * $b;
}
Echo Cheng (10,16);
?>
Five, branch magical
If you have too many branches, use switch. If it's rare, use IfElse.
The writing of slow efficiency
Copy the Code code 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
} ...
?>
High-efficiency notation
Copy the Code code as follows:
<?php
Switch ($a) {
Case 1:
Code Block 1
Break
Case 2:
Code Block 2
Break
Case 3:
Code block 3
Break
...
Default
Default Block
}
?>
PHP as a server-side language, program design is particularly important, to maintain an efficient style will make your program run smoother!
How PHP can reduce server consumption and improve efficiency
There are many ways to optimize PHP, you can from the server, server software such as Apache, database such as MySQL, but the most important thing is to work on the PHP code, change the algorithm faster, reduce operations and so on.
The following is an excerpt:
1. If a method is static, make a static declaration of it. The rate can be increased to 4 times times.
2.echo is faster than print.
3. Use Echo's multiple parameters (meaning: a comma instead of a period) instead of a string connection.
4. Determine the maximum number of loops before executing the For loop, and do not calculate the maximum value once per loop.
5. Unregister those unused variables, especially large arrays, in order to free up memory.
6. Avoid using __get,__set,__autoload as much as possible.
7.require_once () is costly.
8. The full path is used when the file is included, and less time is required to resolve the operating system path.
9. If you want to know when the script starts executing (that is, the server side receives the client request), use $_server[' request_time '] better than time ().
10. function instead of regular expression to complete the same function.
The 11.str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times that of the Str_replace function.
12. If a string replaces a function that accepts an array or character as an argument, and the parameter length is not too long, consider writing an additional replacement code so that each pass argument is a character instead of just one line of code that accepts the array as a parameter for querying and replacing.
13. Use the Select Branch statement (that is, switch case) better than using multiple if,else if statements.
14. Masking error messages with @ is very inefficient.
15. Open the Apache mod_deflate module.
16. The database connection should be switched off when it is finished.
The efficiency of the $row [' ID '] is 7 times times that of $row[id].
18. Error messages are costly.
19. Try not to use functions in the For loop, such as for ($x =0; $x < count ($array), $x) the count () function is called every time the loop occurs.
20. Increment the local variable in the method, the speed is the fastest. is almost equivalent to the speed at which local variables are called in the function.
21. Incrementing a global variable is twice times slower than incrementing a local variable.
22. Incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.
23. Incrementing a non-predefined local variable is 9 to 10 times times slower than incrementing one of the pre-defined local variables.
24. Defining only one local variable without calling it in the function also slows down the speed (which is equivalent to incrementing a local variable). PHP will probably check to see if there are global variables.
25. The method invocation appears to be independent of the number of methods defined in the class, because I added 10 methods before and after the test method, but there was no performance change.
26. A method in a derived class runs faster than the same method defined in the base class.
27. Call an empty function with one parameter, which takes the same amount of time as performing a local variable increment operation of 7 to 8 times. A similar method call takes a time close to 15 local variable increment operations.
28. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for a variable in a string surrounded by double quotes, the single quotation mark does not. Of course, you can do this only if you don't need to include variables in the string.
29. When outputting multiple strings, use commas instead of periods to separate strings, faster. Note: Only echo can do this, it is a "function" that can take multiple strings as arguments (the PHP manual says that ECHO is a language structure, not a real function, so the function is enclosed in double quotes).
30.Apache Parsing a PHP script is 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use fewer scripts.
31. Unless the script can be cached, it will be recompiled every time it is called. Introduce a set of PHP slow ... Remaining full text >>
How to improve efficiency by reading large files in PHP
Mainly see how you use, if only to read it, and then write to another file to go, the speed is not too slow.
Give me a demo address to see:
service.020i.cn/baidu/bigfile.php
http://www.bkjia.com/PHPjc/867251.html www.bkjia.com true http://www.bkjia.com/PHPjc/867251.html techarticle PHP in a few can improve the efficiency of code writing, skill sharing, PHP writing nonsense not much to say, look directly at the code example. One, iterate the array in the traversal array note the use of the count of the Times ...