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