-1

Let's take a look at the following code:

if ($a == 1) {
    echo "this is stage 1";
} 
else if ($a == 2) {
    echo "this is stage 2";
}
else if ($a == 3) {
    $a = 1;
    // at this point I want something that restarts the if-else construct so
    // that the output will be "this is stage 1"
}

I'm working on an if else construct at the moment and let's say that I have three stages and the if-else construct checks which stage I'm in.

Now it happens that some activities in stage 3 lead to a jump back to stage 1. Now I've already passed the code for stage one, which is why I want to somehow restart the if-else construct. Is there a way to do that? And even more important: Is there a better way to do what I want? Because my idea doesn't seem to be good practice.

6 Answers6

2

You're right, it's bad practice.

You're asking for goto.

Example:

<?php
goto a;
echo 'Foo';

a:
echo 'Bar';

The above would never output 'Foo'

It's difficult to suggest the better method without seeing exactly what you're trying to do, but consider a switch.

switch ($a) {

 case 3:
    // Execute 3 stuff
    // No break so it'll continue to 1
 case 1:
   // Execute 1 stuff
   break // Don't go any further
 case 2:
    // 2 stuff
    break; 


}

That's probably not what you want either.

You may just want to abstract the code into functions and call them multiple times if necessary.

Martin
  • 6,632
  • 4
  • 25
  • 28
  • +1 for suggestion `goto` as an answer. +1 for considering it a really bad practise! And finally +1 for _"It's difficult to suggest the better method without seeing exactly what you're trying to do"_. – eyecatchUp Feb 01 '13 at 00:54
2

You can put an endless loop around your if and break out if you're done

while (1) {
    if ($a == 1) {
        echo "this is stage 1";
        break;
    } 
    else if ($a == 2) {
        echo "this is stage 2";
        break;
    }
    else if ($a == 3) {
        $a = 1;
    }
    else {
        break;
    }
}

Maybe you want to look at Wikipedia - Finite-state machine and this question PHP state machine framework

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • This was my second thought if switch() doesn't do what you need. +1 – Emery King Feb 01 '13 at 00:49
  • 1
    I don't think a loop is a good solution here. Maybe I'm misunderstanding the question but it doesn't seem like there is a task being repeated here. The OP wants to execute one a certain amount of code one time, and it may or may not occur after executing a different set of code. `Break` is just a fancy `Goto` when used like this. It also gets hairy when $a isn't 1, 2, or 3. Right now, his code does nothing. With a loop, it hangs, so at the very least he would need to add a default case with a break, which I think, again, is just wrapping a `Goto`. – Cameron Beatty Feb 01 '13 at 00:53
  • I was actually thinking about a loop too. The problem is that within each stage the stage is potentially able to change to the next or previous stage. Now there is certain code (not all) within each of the stages that has to be executed when the stage changes to that stage. So I could make a 2 times loop around my if-else construct to catch this stage change ... – user1601869 Feb 01 '13 at 01:02
  • 1
    ... oh but then it repeats my stage code if the stage doesn't change. I have to think about that... – user1601869 Feb 01 '13 at 01:09
  • @user1601869 If you don't want to repeat, break out. Please see updated answer. – Olaf Dietsche Feb 01 '13 at 01:17
1

The short answer is yes, there is a way, but the better answer is yes to your second question as well.

Put, at very least, the code that can get called from multiple locations in a function. For example,

function stageOneCode() {
    //do stuff;
}

etc.. I would recommend a function for each stage, but it's hard to make recommendations without actually seeing what's being executed in the stages.

In any event, at the end of your stage three function, simply call your stage one function.

0

A recursive function is helpful for this (but maybe overkill if it will always revert back to 1)

function echo_stage($stage) {
    if ($a == 1) {
        return "this is stage 1";
    } 
    else if ($a == 2) {
        return "this is stage 2";
    }
    return echo_stage(1);
}

echo echo_stage(5);

Or:

switch ($number)
{
    case 2 :
        echo "this is stage 2";
        break;
    case 1:
     default:
        echo "this is stage 1"
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

use switch(). you can have a "default" case as well as specific cases.

Emery King
  • 3,550
  • 23
  • 34
0

A loop is what you are searching for:

// initialize $a 
$a = 1;

// the while loop will return endless
while (true);

    // if you want to break for any reason use the 
    // break statement:

    // if ($whatever) {
    //    break;
    // }

    if ($a == 1) {
        echo "this is stage 1";
    }
    else if ($a == 2) {
        echo "this is stage 2";
    }
    else if ($a == 3) {
        $a = 1;
        // continue will go back to the head 
        // of the loop (step 1) early:
        continue;
    }

    // don't forget to increment $a in every loop
    $a++;
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266