View Full Version : Why does die() stop HTML output?

01-07-2008, 04:25 AM

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:

<title>PHP Die() Example</title>
$cheese = "Jack";
if($cheese == "Cheddar"){
echo "This is the best cheese!";
echo "Your taste in cheese disappoints me!");
<div id="footer">Copyright&copy;2008 PHP Die Cheese Example</div>
<img src="super-cheese.png" alt="Cheddar Cheese at Its Best!" />

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 :(

01-07-2008, 05:10 AM
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.

02-19-2008, 10:51 PM
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 ;)

02-20-2008, 12:10 AM
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