Some PHP Coding tips"pending Update"

Source: Internet
Author: User

1. Use list to achieve a specific segment value after getting explode:

   

2, pay attention to = =, switch, In_array of the pine comparison (loose comparision)

For example, in the example below, if the $name value is 0, then it will satisfy any case.

Switch ($name) {case    "danny":        ...        break;    Case "Eve":        ...        break;}

Workaround: Before switch, convert the variable type to the desired type. The In_array provides a third parameter, and if the value of the third parameter is TRUE then the In_array () function also checks to see if the variable is of the same type. (Reference: https://php.net/manual/zh/function.in-array.php)

Switch (Strval ($name)) {case    "danny":        ...        break;    Case "Eve":        ...        break;}

3. Use switch to overwrite if else

For example:

if ($a) {} else if ($b) {} else if ($c | | $d) {}

Can be easily rewritten to be clearer:

Switch (TRUE) {case    $a: Break        ;    Case $b: Break        ;    Case $c: Case    $d: Break        ;}

4, do not use the third variable to exchange the value of two variables

List ($a, $b) = Array ($b, $a);

5. Iterating through an array using Array_map

After retrieving a record from a database, it is often a foreach to fetch one of the fields, so you can use Array_map to simplify the code.

$uids = Array_map (function ($user) {return $user [' id '];}, $users);

Reference:
1, http://www.laruence.com/2011/03/24/858.html
2, Http://get.jobdeer.com/75.card

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