PHP: get back to the beginning of the loop using some kind of “break”?

Hi, I have a loop, and I was wondering if there is a command with which you can go back to the beginning of the loop and ignore the rest of the code in the loop

Example:

for ($index = 0; $index < 10; $index++) { if ($index == 6) that command to go the start of the loop echo "$index, "; } 

should output

1,2,3,4,5,7,8,9 and miss six

kind of the same result as

 for ($index = 0; $index < 10; $index++) { if ($index != 6) echo "$index, "; } 

is there any team for this?

thanks matthy

+4
source share
2 answers

The keyword to use is continue :

 for ($index = 0; $index < 10; $index++) { if ($index == 6) continue; // Skips everything below it and jumps to next iteration echo "$index, "; } 

Alternatively, in order to get the desired result, your for line should read this instead (if you didn't skip zero):

 for ($index = 1; $index < 10; $index++) 
+13
source

Yes, continue proceeds to the next iteration.

+4
source

Source: https://habr.com/ru/post/1334728/


All Articles