note 126874 added to control-structures.goto

From: Date: Thu, 24 Feb 2022 09:52:02 +0000
Subject: note 126874 added to control-structures.goto
Groups: php.notes 
Request: Send a blank email to php-notes+get-217648@lists.php.net to get a copy of this message
I found a good way to use goto for walking through a foreach iteration one another time in order not to walk through whole array once again or not to use special and mostly complex if...else constructions. But don't forget to make an exit from the goto loop if the iteration of rewalking reaches to many attemptions. Brief example: foreach ($fooArray as $foo) { $attemptionLimit = 0; restartIteration: if (++$attemptionLimit > 10) { continue; } $result = $foo->doSomething(); if (!$result) { $foo->doSomethingElse($attemptionLimit); goto restartIteration; } else { echo "Done!"; } } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 92.37.159.184) ---- Manual Page -- https://php.net/manual/en/control-structures.goto.php Edit -- https://main.php.net/note/edit/126874 Del: integrated -- https://main.php.net/note/delete/126874/integrated Del: useless -- https://main.php.net/note/delete/126874/useless Del: bad code -- https://main.php.net/note/delete/126874/bad+code Del: spam -- https://main.php.net/note/delete/126874/spam Del: non-english -- https://main.php.net/note/delete/126874/non-english Del: in docs -- https://main.php.net/note/delete/126874/in+docs Del: other reasons-- https://main.php.net/note/delete/126874 Reject -- https://main.php.net/note/reject/126874 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217648) next »