Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm about to use a goto statement

Tags:

php

Save me from raptor death - is there any better way to handle this kind of structure?

while(condition) {
    $this->phase1();
    $this->phase2();
    $this->phase3();
    $this->phase4();
}

Throughout either one of those methods, the condition could be met. IMMEDIATELY after the condition is met, the loop MUST exit. If I could call break; inside of phase2(); for example, I wouldn't need a goto statement (but of course, that would throw an error).

like image 996
Chris G. Avatar asked Sep 28 '11 20:09

Chris G.


People also ask

What is goto statement used for?

The goto statement can be used to alter the normal flow of control in a program. This statement causes the program to jump to a specified label.

Should you use goto statements?

"The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."

What can be used instead of goto statement?

From the early days of programming (i.e. assembler programming) the replacement for goto directives is so-called structured programming. This means using if-then-else statements, for- and while-loops, switch statements, break and continue, etc.


1 Answers

Return a boolean to execute each stage until successful.

while (condition) {
    if ($this->phase1() || $this->phase2() || $this->phase3() || $this->phase4()) {
        // Success!
    }
}
like image 134
Brad Koch Avatar answered Oct 11 '22 12:10

Brad Koch