Exit; within include()

Discussion in 'PHP' started by refined, Jul 2, 2008.

  1. #1
    I have a script:

    mainfile.php:
    
    <?
    echo "Hello world!";
    
    include ("file2.php");
    echo "Blah blah blah";
    echo "Do other things..";
    ?>
    
    Code (markup):
    file2.php:
    
    <?
    $yes=1;
    if($yes == "1") {
     echo "<br>How are you?";
     exit;
    }
    ?>
    
    Code (markup):
    I basically want that "exit;" to be the end-all and cut the rest of the script (inside mainfile.php).

    This is not the actual script, just a re-enactment of the problem.

    Feedback/help appreciated!

    -Refined
     
    refined, Jul 2, 2008 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not use die()?
     
    TwistMyArm, Jul 2, 2008 IP
  3. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #3
    2refined: I cannot not see any problems with given examples, exit() works as it should - it terminates the whole script.

    The die() function is just an alias for exit().
     
    wmtips, Jul 3, 2008 IP
  4. wowla_123

    wowla_123 Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    He is right. When you include a file in another file, the PHP interpretor treats it as one file. In this case, following is the code that PHP interprets:

    <?
    echo "Hello world!";
    
    $yes=1;
    if($yes == "1") {
     echo "<br>How are you?";
     exit;
    }
    echo "Blah blah blah";
    echo "Do other things..";
    ?>
    Code (markup):
    So, no code will be executed after exit().
     
    wowla_123, Jul 3, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Yes, this should indeed stop the whole file.

    If you just wanted to cut the included file short, then you can use return in the global scope.
     
    Danltn, Jul 3, 2008 IP