10 php programming tips and 10 php tips _ PHP tutorials

Source: Internet
Author: User
10 php programming tips and 10 php tips. 10 php programming tips, 10 php tips 1. this is used when writing a program, for example, rounding a number. Many people write like this: Copy the code with 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:
The 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.
The code is as follows:
A = fix (a + sgn (a) * 0.5)

Written in php:
The 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.
The 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.
The 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.

Example 1: This is used when writing a program, for example, rounding a number to an integer. Many people write the code like this: code...

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.