Exit PHP, Continue With Output

Discussion in 'PHP' started by MMJ, Sep 12, 2008.

  1. #1
    Let say I have something like

    
    <?php
    //code
    ?>
    <html />
    
    PHP:
    Is there a way to stop the PHP from running and just output the text. Like:


    
    <?php
    //code
    if ($condition)
    exitphp;
    ?>
    <html />
    
    PHP:
     
    MMJ, Sep 12, 2008 IP
  2. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Then use braces e.g.

    <?php
    //code
    if(!$condition)
    {
    continue running php script
    }
    ?>
    <html />
     
    allaboutgeo, Sep 12, 2008 IP
  3. ignas2526

    ignas2526 Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    do you mean what:
    <? if ($condition) { ?> <html /> <?} else { die();}?>
    in what example if $condition is something wan it will post <html /> else it will stop executing.
    Ore you want to execute html only if not condition, wan:
    <? if ($condition) { die(); }else {?> <html /> <?}?>
    In what example if condition, it will die, and if else it will post <html />.
     
    ignas2526, Sep 12, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, I know that I can do that but I was wondering if there is another way, without having to put the code in the if structure.
     
    MMJ, Sep 13, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Possibly. If you used something like:
    $me = file_get_contents(__FILE__); // __FILE__ is a PHP defined constant
    $me = explode('?>', $me); // etc.
    echo $me[/* Continue here */];
    
    PHP:
    Bit of a complicated way I think, but it can be done.

    Jay
     
    jayshah, Sep 13, 2008 IP
  6. raghav

    raghav Active Member

    Messages:
    716
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Try Using Break; ?
     
    raghav, Sep 13, 2008 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That doesn't work.

    I also tried return, but it worked just like exit.

    Heh, interesting idea.
     
    MMJ, Sep 13, 2008 IP
  8. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I know this is a bit silly but if there are any more suggestions so... bump. :)

    I tried return but it worked like exit.
     
    MMJ, Sep 20, 2008 IP
  9. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hey this is nonsense , why do you need that ?
    if you need so separate PHP part from HTML
    
    <?php
    //code
    
    include('htmlfile.htm');
    ?>
    
    PHP:
    so you call directly htmlfile.htm
     
    classic, Sep 20, 2008 IP
  10. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think return will do what you want if the PHP is in a separate file. I think this is what classic's post is getting at.

    PHP6 might make you happy. :)

    http://www.developertutorials.com/blog/php/goto-is-coming-to-php-14/
     
    LogicFlux, Sep 20, 2008 IP
  11. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #11
    This doesn't really solve my problem. :confused:

    I don't really see how GOTO can fix my problem. :confused:
     
    MMJ, Sep 20, 2008 IP
  12. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I was thinking maybe you'd be able to do something like this:

    
    <?php
    
    // stuff here
    
    
    if ($condition)
      break goto_html; 
    
    // more code here
    
    goto_html: 
    ?>
    <html>
    
    
    
    
    PHP:
    But I'm really not sure how it will work.

    Wow, goto is actually in PHP 5.3. http://wiki.php.net/doc/todo/undocumented#php_5.3
     
    LogicFlux, Sep 20, 2008 IP
  13. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #13
    Here is how it can be done:

    1) Using conditional statement like stated above (if/switch)
    2) Keep the PHP code in a file, include it:

    
    <?php
    include('file.php');
    ?>
    HTML
    
    PHP:
    In file.php, use return to stop executing the file from continuing.

    3) Use a loop (for/while/do) and use continue/break to exit them.

    Peace,
     
    Barti1987, Sep 20, 2008 IP
  14. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #14
    Why don't you make a function which processes a file whih html , and call it just before exit();
    Ex:
    function myfile($f){
    ob_start();
    include($f);
    $str= ob_get_contents();
    ob_end_clean();
    return $str;
    }

    Now in your php file:
    if ($condition){ echo myfile('somefile.php'); exit(); }

    The variable in php file will not be available in function, unless you set a global $var inside function. Easiest will be to add these variables to $GLOBALS and then call the function.
    regards :)
     
    JEET, Sep 21, 2008 IP
  15. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #15
    You can end the php usng ?> and than write the html and begin it again wherever u need it u can do it using break
     
    Bohra, Sep 21, 2008 IP
  16. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #16
    use if-else statement? easier..
     
    ads2help, Sep 21, 2008 IP