10 PHP Programming tips, 10 PHP tips _php Tutorial

Source: Internet
Author: User

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 ...

  • Related Article

    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.