Search code examples
phpreturnkilldie

PHP: kill script or premature return?


In PHP, I can use die() to kill the whole script. However, I want the script to execute up to a certain point and quit without the whole script dying.

Something like this:

echo "Hello, what is your name?";
if($name == $blacklist)
{
   echo "I don't talk to strangers";
   die(); // This will break all the echos
}
else
  // Stuff

I just want the script to terminate (and print the echos up to the point of termination), and using die() will actually make the script print nothing.

Any ideas? Maybe "return 0" like in C?


Solution

  • You can do:

    die("I don't talk to strangers");
    

    Or:

    echo "I don't talk to strangers";exit;
    

    Or simply "return" return;

    Or throw new Exception("I don't talk to strangers");