Home > Article > Backend Development > php foreach/for loop jump problem_PHP tutorial
In PHP, for loop and foreach are two commonly used functions. for is used for large numbers, while foreach is generally used for array traversal.
The code is as follows
|
Copy code | ||||
//The current loop of php is 1, and the loop increases from the inside to the outside. The default break is 1, for example, jumping out of the second layer of the loop
for ($i=0;$i<3;$i++){
foreach (array(1,2,3) as $val){
foreach (array(1,2,3) as $val){
echo "1 layer loop
";
break 2;
}
echo "2-layer loop
";
}
echo "3-layer loop
";
}
//Result:
//1 layer loop
//3-layer loop
//1 layer loop
//3-layer loop
//1 layer loop
//3-layer loop