PHP: kill script or premature return?

11,548

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");

Share:
11,548
dukevin
Author by

dukevin

.-"-.__ | .' / ,-| ```'-, || . ` / /@)|_ `-, | / ( ~ \_```""--.,_', : _.' \ /```''''""`^ / | / `| : / \/ | | . / __/ | |. | | __/ / | | | "-._ | _/ _/=_// || : '. `~~`~^| /=/-_/_/`~~`~~~`~~`~^~`. `> ' . \ ~/_/_ /" ` . ` ' . | .' ,'~^~`^| |~^`^~~`~~~^~~~^~`; ' .-' | | | \ ` : ` | :| | : | |: | || ' | |/ | | : |_/ | . '

Updated on June 04, 2022

Comments

  • dukevin
    dukevin almost 2 years

    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?