Never mind, goto works just fine for something this simple.
... and this... is what starting out as a PHP programmer does to you. God that language sucks.
EDIT: I suppose I should back that comment up, as it could be misleading to the OP. First off, that was by no means a shot at you, or your choice to use PHP. I do however find that PHP is a terribad language, especially for beginners.
As to the use of a goto, they are almost universally maligned. The general reason is they make code incredibly difficult to comprehend and are an easy way to introduce insidious bugs to your code.
In C#, there are only a few cases where C# goto makes sense.
1- generated code, code that is not intended to be read by a human.
2- C# does not support switch statement case fall through, like many other languages. You can however recreate the effect using gotos (*)
3- exit condition within a series of nested loops.
4- in a very very very rare set of cases, for performance reasons, often to eliminate the need for a flag variable.
2 and 3 should both be solvable with code refactoring.
(*) Example of 2 in action:
goto case 2;
EDIT2: As to my comments about PHP, this post sums it up nicely. This analogy is about as apt as I've ever read:
I can’t even say what’s wrong with PHP, because— okay. Imagine you have uh, a toolbox. A set of tools. Looks okay, standard stuff in there.
You pull out a screwdriver, and you see it’s one of those weird tri-headed things. Okay, well, that’s not very useful to you, but you guess it comes in handy sometimes.
You pull out the hammer, but to your dismay, it has the claw part on both sides. Still serviceable though, I mean, you can hit nails with the middle of the head holding it sideways.
You pull out the pliers, but they don’t have those serrated surfaces; it’s flat and smooth. That’s less useful, but it still turns bolts well enough, so whatever.
And on you go. Everything in the box is kind of weird and quirky, but maybe not enough to make it completely worthless. And there’s no clear problem with the set as a whole; it still has all the tools.
Now imagine you meet millions of carpenters using this toolbox who tell you “well hey what’s the problem with these tools? They’re all I’ve ever used and they work fine!” And the carpenters show you the houses they’ve built, where every room is a pentagon and the roof is upside-down. And you knock on the front door and it just collapses inwards and they all yell at you for breaking their door.
That’s what’s wrong with PHP.
Edited by Serapth, 23 October 2013 - 02:14 PM.