Last update: 2011/04/02
1. Use list to achieve a specific segment value after getting explode:
List (, $mid) = explode ('; ', $string);
2. Replace is_null with NULL = = =:
Is_null and NULL = = = are exactly the same effect, but save one function call at a time.
3. Use = = = Try not to = =:
PHP has two sets of equality comparison operators ===/!== and ==/!=, ==/!= will have implicit type conversions, and ===/!== will strictly compare the two operations with the same type and equal values.
We should try to use = = = rather than = =, except because the conversion rules more difficult to remember, but also if the use of = = =, for future maintenance or reading your code will be very comfortable: "At this moment, this line of statements, this variable is this type of!".
4. Use less/no continue:
Continue is going back to the head of the loop, and the loop is going back to the head of the loop, so with proper construction, we can avoid the use of this statement, making efficiency better.
5. Beware of Switch/in_array comparisons (loose comparision):
Both switch and In_array are loosely compared, so it's easy to make mistakes when the types of variables to compare are different:
Copy CodeThe code is as follows:
Switch ($name) {
Case "Laruence":
...
Break
Case "Eve":
...
Break
}
For the switch above, if $name is the number 0, then it will satisfy any case. Likewise in the In_array.
The solution is to convert the variable type to the type you expect before switch.
Copy CodeThe code is as follows:
Switch (Strval ($name)) {
Case "Laruence":
...
Break
Case "Eve":
...
Break
}
Instead, In_array provides a third optional parameter that allows you to change the default comparison method.
6. Switch is not just for distinguishing variables:
For example, for the following section of code:
Copy CodeThe code is as follows:
if ($a) {
} else if ($b) {
} else if ($c | | $d) {
}
Can be easily rewritten as:
Copy CodeThe code is as follows:
Switch (TRUE) {
Case $a:
Break
Case $b:
Break
Case $C:
Case $d:
Break
}
Does it look clearer?
7. The variable is first defined and then used:
Using an undefined variable is 8 times times slower than using a well-defined variable!
Can be similar, the PHP engine will first take the normal logic to get this variable, but this variable does not exist, so the PHP engine needs to throw a notice, and enter a section of the use of undefined variables should go logic, and then return a new variable.
In addition, reading the code from the point of view, when you use an undefined variable, will let the person reading your code confused: "This variable is initialized there, and the previous code is related?" Does it have anything to do with the file included in the include? "
Finally, from the point of view of normative programming, you also need to do this.
8. Swap the values of the two variables without a third variable:
List ($a, $b) = Array ($b, $a),
But in fact, there are anonymous temporary variable generation, for integers, the use of reciprocal operations to do, or more reliable:
Copy CodeThe code is as follows:
$a = $a + $b;
$b = $a-$b;
$a = $a-$b;
However, it is better to use XOR, because +–*/prone to loss of precision or overflow.
9. Floor = = Two non-operation (this article is provided by Skiyo)
Copy CodeThe code is as follows:
Echo ~~4.9;
echo Floor (4.9);
The speed of two non-operations is basically 3 times times that of floor, but there is a point where overflow may occur for large numbers:
Copy CodeThe code is as follows:
Echo ~~99999999999999.99; 276447231
echo Floor (99999999999999.99); 99999999999999
Do{}while (0) Magical (this article is provided by Qianfeng)
We know that Do{}while (0) has a lot of magical things, such as eliminating goto and defining blocks of code in C + +.
So, PHP in the same vein, you can also use Do{}while (0) to do some clever application
Copy CodeThe code is as follows:
do{
if (true) {
Break
}
if (true) {
Break
}
} while (false);
Better
if (true) {
} else if (true) {
} else {
}
11. Use the @ ERROR suppressor as sparingly as possible
The following code:
Copy CodeThe code is as follows:
@func ();
Equivalent (see in-depth understanding of PHP principles for error suppression and inline HTML):
Copy CodeThe code is as follows:
$report = error_reporting (0);
Func ();
Error_reporting ($report);
In addition, the error suppression symbol, may cause some problems, see (http://www.jb51.net/article/27022.htm);
Finally, the error suppressor can cause problems when debugging errors.
12. Try to avoid recursion (this article is from Lazyboy)
Recursive performance is worrying, and most of the recursion is the tail recursion, can be eliminated.
Copy CodeThe code is as follows:
function f ($n) {
if ($n = 0) return 1;
Return $n * F ($n-1);
}
Into:
$result = 1;
for ($y = 1; $y < $n + 1; $y + +) {
$result *= $y;
}
13. Use $_server[' request_time ' instead of Time ()
Time () leads to a function call, and if the exact value of the timing is not high, you can use $_server[' request_time ' instead, much faster.
14. Avoid doing operations in the for-judging condition (this anonymous from the message)
The following code:
for ($i =0; $i }
Causes each loop to call strlen, instead
For ($i =0, $j =strlen ($STR), $i < $j; $i + +) {
}
15. Avoid using regular (this article from Pangyontao)
Regular time-consuming, as far as possible, and the use of direct string processing functions instead, such as:
Copy CodeThe code is as follows:
if (Preg_match ("!^foo_!i", "Foo_")) {}
To be replaced by:
if (!strncasecmp ("Foo_", "Foo_", 4)) {}
if (Preg_match ("![ A8f9]! "," Sometext ")) {}
To be replaced by:
if (STRPBRK ("A8f9", "Sometext")) {}
if (Preg_match ("!string!i", "text")) {}
To be replaced by:
if (Stripos ("Text", "string")!== false) {}
Wait a minute.
16. Use curly braces to enclose variables in double quotes and Heredoc
The following code:
echo "$name [2]";
PHP does not know that the programmer's intentions are $name. "[2]" or $name[2],
So the proposal, all plus braces:
Copy CodeThe code is as follows:
echo "{$name}[2]";
Or
echo "${name}[2]";
17. Use False to indicate an error, and null to indicate that it does not exist.
For a function of the action class, the failure returns false, indicating "The operation failed", and for a function of the query class, if the desired value cannot be found, it should return null, which means "not found".
http://www.bkjia.com/PHPjc/323227.html www.bkjia.com true http://www.bkjia.com/PHPjc/323227.html techarticle last update: 2011/04/02 1. Use list to achieve a specific segment value after getting explode: list (, $mid) = explode ('; ', $string); 2. Use NULL = = to replace is_null:is_null and null = = = Finish ...