Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    Why does die() stop HTML output?

    Hello,

    I've been wondering (and searching) for a while now, why does die() stop HTML output? I thought it could only control the rest of the PHP script, not the rest of the page. For example:
    PHP Code:
    <html>
    <head>
    <title>PHP Die() Example</title>
    </head>
    <body>
    <?php
    $cheese 
    "Jack";
    if(
    $cheese == "Cheddar"){
     echo 
    "This is the best cheese!";
    }else{
     echo 
    "Your taste in cheese disappoints me!");
     die();
    }
    ?>
    <div id="footer">Copyright&copy;2008 PHP Die Cheese Example</div>
    <img src="super-cheese.png" alt="Cheddar Cheese at Its Best!" />
    </body>
    </html>
    Now if I were to execute that, the copyright and image would not show up because the $cheese isn't cheddar. Does anyone know why the copyright and image would not show up?

    This has been puzzling me for a while

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,111
    Thanks
    2
    Thanked 326 Times in 318 Posts
    The rest of the HTML code on that page is part of that PHP page, it just happens to be after and outside of a ?> closing tag. It gets parsed and output by the php language interpreter. There is nothing special about in-line HTML content.

    Short answer - stop means stop. The php interpreter stops when a die/exit statement is executed.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Feb 2008
    Location
    Australia baby!
    Posts
    143
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Yeah you have said die() so it does not show your html underneath mate. Set $cheese to cheddar and it will show copyright, since it doesn't die in that case. If you always wanted the copyright to display, regardless of the value of $cheese, simply remove the die(); line. Die means no more work to be done whatsoever
    outseeker - http://outer.reaches.dyndns.org/index.php
    I hope this helped someone. Due to this forums lame rep system penalising me for nothing, and admins inability to do anything about it, I'm sorry to say I will no longer be online here. Pride is important. Good Luck All.

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    there are very, very few situations where calling die or exit is a good idea - it's a loss in flexibility that's hard to get back later, should you want to
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •