10 php programming tips and 10 php tips
1. This is used when writing a program, for example, rounding a number to an integer. Many people write as follows:
Copy codeThe Code is as follows:
Input
If a-int (a)> = 0.5 then
A = a + 1
End if
In fact, this judgment statement can be written using a very simple expression.
Copy codeThe Code is as follows:
A = fix (a + sgn (a) * 0.5)
Written in php:
Copy codeThe Code is as follows:
$ A = intval ($ a + 0.5 * ($ a> 0? 1:-1 ));
Analysis:
Assume that a is 4.4, then a + 0.5 = 4.9 intval () is 4, and assume that a is 4.6 a + 0.5 = 5.1, then intval () is 5, so rounding is realized.
Positive Value + 0.5, negative value-0.5.
The same is true if two decimal places are retained.
Copy codeThe Code is as follows:
$ A = intval (a * 100 + 0.5 * ($ a> 0? 1:-1)/100.
2. If the value of a-B is less than 0, the value is 0.
Copy codeThe Code is as follows:
$ Result = max (0, $ a-$ B );
3. When importing data, you can use php in csv format to easily process getcsv.
4. str_replace () is more efficient than regular expressions in string replacement.. In fact, according to Making the Web, str_replace () is 61% more efficient than regular expressions like ereg_replace () and preg_replace.
5. if ($ a = true) if ($)
6. Use isset to determine whether variables and elements exist at a high speed
7. Try to use the three-object operation
8. Write the if statement as appropriate. Use the return statement in the function to reduce the branch size.
9. Use memcache mogodb to reduce the burden on programs and databases
10. You can use sqllite to record temporary data.