The content of this article is a summary of knowledge about PHP process control (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
There are three ways to traverse arrays in PHP: for loop, foreach loop, while, list(), each() combined loop
Arrays in PHP are divided into: index array [converted to json is an array] and associative array [converted to json is an object]
For loop can only traverse index arrays, foreach can traverse index arrays and associative arrays, while, list(), and each() combined loops can also traverse index arrays and associative arrays
While, list(), and each() combination will not reset() the array pointer
foreach traversal will reset() the array )Operation
php branch: if...elseif (a basic principle: put the most likely conditions first for processing)
php branch: switch...case... (the data type of the control expression behind switch can only be: integer, floating point type or string), use the continue function in switch and Same as break, to jump out of the switch outer loop, use continue num, break num, break num is to end the entire loop body of the numth outer layer, continue num is to end the single loop of the outer numth layer
switch...case...in PHP will generate a jump table (underlying usage principle), jump directly to the corresponding case, unlike if elseif, which goes through layers of judgments
Tips for improving the efficiency of branch judgment: If the judgment is more complex and only integers, floating point types or strings are judged, you can use switch processing, which will improve efficiency
Proof example:
<?php $arr = ["apple", "pear", "banana", "orange", "lemon", "strawberry"]; ; end($arr); //数组指针指向最后一个值 var_dump("打印当前数组指针对应的值:".current($arr)); //打印当前数组指针对应的数组 foreach ($arr as $key => $val){ var_dump("打印foreach循环当前数组指针对应的值:".$val); if($key == 3){ break; } } var_dump("打印当前数组指针对应的值:".current($arr)); //打印当前数组指针对应的数组 while($element = each($arr)) { var_dump($element); } //输出结果: string '打印当前数组指针对应的值:strawberry' (length=49) string '打印foreach循环当前数组指针对应的值:apple' (length=57) string '打印foreach循环当前数组指针对应的值:pear' (length=56) string '打印foreach循环当前数组指针对应的值:banana' (length=58) string '打印foreach循环当前数组指针对应的值:orange' (length=58) string '打印当前数组指针对应的值:lemon' (length=44) array (size=4) 1 => string 'lemon' (length=5) 'value' => string 'lemon' (length=5) 0 => int 4 'key' => int 4 array (size=4) 1 => string 'strawberry' (length=10) 'value' => string 'strawberry' (length=10) 0 => int 5 'key' => int 5
The above is the detailed content of Knowledge summary of PHP process control (with examples). For more information, please follow other related articles on the PHP Chinese website!