10 PHP Programming tips, 10 PHP tips
1, write the program will use this situation, such as a number rounded rounding. A lot of people would write this:
Copy the Code code as follows:
Input A
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 the Code code as follows:
A = fix (A + SGN (a) *0.5)
Writing in PHP:
Copy the Code code 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 () after 4, assuming a is 4.6 a+0.5=5.1 then Intval () is 5, so rounding is achieved.
Positive number +0.5, negative number-0.5.
If rounding retains 2 decimal places, the same is true.
Copy the Code code as follows:
$a = intval (A * + 0.5 * ($a >0? 1:-1))/100.
2, if the value of a-B, if less than 0 take 0, you can write the city
Copy the Code code as follows:
$result = max (0, $a-$b);
3, import data, you can choose to use the CSV format PHP getcsv processing is very convenient.
4. Str_replace () singular expression is more efficient in replacing strings . In fact, according to making the Web, the Str_replace () is 61% more efficient than regular expressions like ereg_replace () and Preg_replace ().
5. if ($a ==true) if ($a)
6. Use Isset to determine whether variables and elements exist at a higher speed
7, try to use three mesh operation
8. Write the IF line statement appropriately. Use return statements to reduce branching within function
9, use Memcache Mogodb and so on to reduce the program and the database burden
10, temporary data can use Sqllite record
http://www.bkjia.com/PHPjc/1028303.html www.bkjia.com true http://www.bkjia.com/PHPjc/1028303.html techarticle 10 PHP Programming tips, 10 PHP tips 1, write the program will use this situation, such as a number rounded rounding. Many people will write this: Copy code code ...