10 PHP Programming Tips _php Tips

Source: Internet
Author: User

1, write the application of the time will use this situation, such as a number rounded rounding. A lot of people would write this:

Copy 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 Code code as follows:

A = fix (A + SGN (a) *0.5)

Writing in PHP:
Copy Code code as follows:

$a = intval ($a + 0.5 * ($a >0 1:-1));

Analysis:

Suppose 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 () then 5, thus rounding is achieved.
Positive +0.5, minus 0.5.

The same is true if rounding is reserved for 2 decimal places.

Copy 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 then take 0, you can write the city
Copy Code code as follows:

$result = max (0, $a-$b);

3, import data, you can choose to use CSV format PHP for getcsv processing is very convenient.

4. Str_replace () Buzheng expressions are more efficient in substitution strings . In fact, according to the making web, Str_replace () is 61% more efficient than regular expressions like ereg_replace () and Preg_replace ().

5. if ($a ==true) if ($a)

6, using Isset to determine whether the variables and elements are high speed

7, try to use three mesh operation

8, appropriate to write if line statements. Use return statements to reduce branches appropriately within a function

9, use Memcache Mogodb and so on to reduce the program and the database burden

10, temporary data can use Sqllite record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.