Comprehensive Interpretation php familiar - Process Control

First, the three ways PHP through the array

Example:

$arr = [1, 2, 3 4, 'five' => 5];

1、for () 

 can only be used to traverse the loop for pure indexed arrays ! If there is an associative array, count counts the total number of two kinds of arrays when statistics for the use of loop through the array of mixing, resulting in an array out of bounds! !

$num = count($arr);       
for($i=0;$i<$num;$i++){
   echo "{$i}=>{$arr[$i]}<br/>";
}

2、foreach()

Can traverse any form of an array, I not here, for example, and it is too common.

3、while list()、each()

Can traverse the index and associative arrays, but its drawback is the next use is required REST () resets pointer array .

Array using each () traversed again after the next pointers in the last bit; that is, then each (), always return false ;
If you need to use, required reset ($ arr); function to reset the array pointer.

while(list($key,$value) = each($arr)){
   echo "{$key}=>{$value}<br>";  
}
reset($arr);

二、if ..... elseif....else

In the if ...... elseif ...... else statements which, of course, the middle may have multiple elseif, because only one statement pieces to be executed, we need to follow a principle: the possibility of large on the front

三、switch......case

For the switch ($ var), we need to pay attention to is:

1, $ var can only be integer, floating point, and string .

2, Continue statement break statement and function in the same switch.

3, out of the loop if desired Switch outside, may be used Continue 2 .

This article is a summary of kangaroos in the study, if reproduced, please indicate the source: https://www.cnblogs.com/chrdai/p/11075204.html

 

Guess you like

Origin www.cnblogs.com/chrdai/p/11075204.html