10 php programming tips and 10 php tips

Source: Internet
Author: User

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:
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 ));


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.

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.