0

Is there a better way than goto to achieve this:

index.php:

echo "Welcome to our site";
require 'showPonies.php';
echo "I hope you enjoyed your stay!";

showPonies.php:

...
if (isset($_GET['noponies'])
  goto EXITSCRIPT;
...

// This is the end of the file
EXITSCRIPT:
?>

Specifically I am looking for a way inside showPonies.php to "exit(0)" the script and return processing from where it was included/required.

William Entriken
  • 37,208
  • 23
  • 149
  • 195
  • 3
    That's what `return` already does. – mario Mar 19 '14 at 19:46
  • What your doing is correct. You could have your 'exitscript:' in index.php if you wanted. – Matt The Ninja Mar 19 '14 at 20:03
  • @MattTheNinja It is "correct" in the extremely limited sense that it will have the desired effect. It is *not* correct in that "goto considered harmful". Also, [raptors!](http://php.net/goto) – IMSoP Mar 19 '14 at 20:42
  • @mario You say that like nobody could possibly not know that. I've been programming in PHP for years, and only recently came upon that use of `return`. – IMSoP Mar 19 '14 at 20:44

1 Answers1

2

You can use PHP's return construct for this:

if (isset($_GET['noponies']))
    return;

From the doc:

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102