PHP what if continue 2 and break both in switch case statement -
PHP what if continue 2 and break both in switch case statement -
i new php. saw below code online, comes go on 2 , break in switch case statement. mean?
foreach ( $elements &$element ) { switch ($element['type']) { case : if (condition1) go on 2; break; case b : if (condition2) go on 2; break; } //remaining code here in loop outside switch statement }
from php.net:continue:
continue accepts optional numeric argument tells how many levels of enclosing loops should skip end of. default value 1, skipping end of current loop.
php.net:switch
if have switch within loop , wish go on next iteration of outer loop, utilize go on 2.
php continues execute statements until end of switch block, or first time sees break statement. if don't write break statement @ end of case's statement list, php go on executing statements of next case.
explanation: continue 2
jumps next iteration of loop @ 2 levels back, foreach
. break
(equivalent break 1) ends current loop, switch
.
in english: each $element, if type "a" , condition1 met or if type "b" , condition2 met, go on next $element , ignore rest of switch options. otherwise, go on next switch alternative or, if lastly switch option, execute code after switch.
here's demonstration.
php
Comments
Post a Comment