Could someone please explain, with examples, what is meant by loop break 2 or continue 2 in PHP? What does it mean when break or continue is followed by a number?
Could someone please explain, with examples, what is meant by loop break 2 or
Share
outputs “1” because the loop was broken forever, before echo was able to print “2”.
outputs
13because the second iteration was passedoutputs
ABbecause ofbreak 2, which means that both statements was broken quite early. If this was justbreak, the output would have beenAB1AB2AB3.will output
ABABAB, because ofcontinue 2: the outer loop will be passed every time.In other words,
continuestops the current iteration execution but lets another to run, whilebreakstops the whole statement completely.So we can ell that
continueis applicable for the loops only, whereasbreakcan be used in other statements, such asswitch.A number represents the number of nested statements affected.
if there are 2 nested loops,
breakin the inner one will break inner one (however it makes very little sense as the inner loop will be launched again in the next iteration of the outer loop).break 2in the inner loop will break both.