user960725
user960725

Reputation: 11

die() but only within the <?php ?> tags?

If I have code such as:

<html>
...
<?php
....
die();
?>
....
</html>

then the entire page execution stops, and the final html does not get shown. is there a way to only leave the php within the enclosing php tags?

Upvotes: 0

Views: 98

Answers (4)

Michael Berkowski
Michael Berkowski

Reputation: 270677

Don't ever call die() inside an HTML script. If you are calling die() as part of mysql_query() error checking, for example, you should instead wrap the call in an if() statement. This doesn't only apply to MySQL, of course. You can use this in place of any instance where you would call die().

// Instead of this...
$result = mysql_query() or die();

// Do this...
$result = mysql_query(...);
if (!$result) {
  $errorstring = "an error occurred producing your data";
}

It is generally considered bad practice to include a lot of application logic within the HTML. Instead do these things at the top of the script and store the output in variables for reuse later.

Upvotes: 4

macintosh264
macintosh264

Reputation: 981

what you should do is have an external php script and iframe it into the page, or use jquery to ajax it in. Then if it dies nothing will be displayed in the iframe, or div (if you use jquery), but you can still die

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96266

I guess this is based on same condition, otherwise it wouldn't make sense.

<html>
...
<?php
....
if (! condition) { //skip if not needed
  ...
}
?>
....
</html>

Upvotes: 6

genesis
genesis

Reputation: 50974

You can't die() and expect execution will continue. That is illogical and impossible

Upvotes: 3

Related Questions