-1

I know goto statements are bad, but I find that it is hard to not use them when programming. I am a new programmer, and I know they are bad practice, but what are some ways to get around using them? I know about IF ELSE statements, but what other tools are out there to help me avoid using GoTo's?

blazerunner44
  • 657
  • 8
  • 19
  • 2
    Are you really, *really*, **really**, ***reeeeeallly*** sure you want to use a `goto`? That is a slippery slope my friend. –  Jul 18 '14 at 00:53
  • 3
    Pleeeeeeeease don't use `goto`. [Think of the children](http://goo.gl/3BEpSI) – Darren Jul 18 '14 at 00:54
  • 1
    How would I jump to another section of my file using something other than goto? – blazerunner44 Jul 18 '14 at 00:55
  • 3
    If you design your code properly you won't have to. –  Jul 18 '14 at 00:58
  • 1
    What exactly would you need to do when you got to `a`.. ? – Darren Jul 18 '14 at 00:58
  • 1
    @Darren There is more PHP code to be executed after the sample I gave you. I would like to jump out from executing the rest of the code and go straight to the HTML content which is even farther down. – blazerunner44 Jul 18 '14 at 01:01

1 Answers1

5

Goto is a feature new, since PHP 5.3. There is some very, very specific reasons to use them. You will find some Goto's in drivers or kernel codes, but I really can't see any reason to use it in usual CMS, Blog, Social Network, e-shop and so on...

If you will return a 404, you can use a "header('errorpage.php')", for instance. You can assign a flag.

} elseif ($event['response']->getStatusCode() == 404) {
  $errorno = '404';
  //goto a;
  $thingsGoingWrong = true;
}         

. .

<?php if ($thingsGoingWrong) { doBazinga(); } ?>

Considering you are not using a prior version of PHP, there are some rules for Goto, like you can't enter in a loop, or switch statement.

Gurgel
  • 61
  • 3