Hi, folks.

As you may have gathered from a previous post, I know next to nothing about PHP, so I have a very basic question:

How do I force an exit from PHP?

I have gone through a few books and come across the "die" and "exit" commands; however, they don't seem to be doing what I want.

What I'd like to do: if a condition is true, exit PHP execution and display the rest of the (HTML) page normally. I suppose I could create a big block for the case if the condition is not true; however, a quick exit from the PHP would be my preferred option. Presently, using the "die" or "exit" commands with a test condition that forces the error causes the entire webpage to display a blank page, not even the error message echoed BEFORE the "die" statement displays.

Any suggestions?

David

Ummm, from you post I am not entirly sure what you are trying to achieve. At a guess I think that you are trying to increase the speed and remove and uncessary server load. I point you to the purpose of the <? ?> tags. These turn on an off the php processor.

So

<html>
<body>
<? if(true) {?>
<p>My True Statment</p>
<? }else {
//Some php Code
}?>
</body>
</html>

I hope this helps,

Urban

well if you are always going to be using the same html code for a given page, then what you could do is, put it in a different page and then just include it wherever needed, for example...

<?
//a lot of php code here
if (something_isnt_true) {
     include 'html_code.html.inc';
     exit();
}

//continue doing the rest of your php code here!

?>

<!- maybe a little more html if needed -->

hope this helps!

Thanks for the replies.

I am working on a PHP parser that I had worked on some time ago, set aside, and am now getting back to.

So far the newsfeed works fine. However, JUST IN CASE the feed ever goes down, I would like the rest of the page to display properly. At the moment, I am working on a test page and am deliberately feeding it a bad link to test as I debug. So far, I keep getting a blank page.

UrbanSky, your suggestion is pretty slick; I hadn't thought if that. Is the syntax proper (would it actually work)? In any case, I suspect that by turning off the PHP, the following PHP code would be displayed in the browser.

For example, from your example, if the condition were true and the PHP turned off, wouldn't the following code be displayed as text in the browser (something I don't want):

<p>My True Statment</p>
<? }else {
//Some php Code
}?>

The syntax works,

What will happen is that the php hits the second <? tag it turns back on see the end of the if closing bracket of the if statment evaluates the else statment ie it know that it is not going to use it so then skips to the end of the if statement and the turns php off again. It is an effiecnt way to code this kind of thing. In heavy load test turning php on and off always outperforms full php scripts that use echo or print staments to achieve the same effect.

I just came around the same questions and found that a construct of the form

...
If ($noUrl)
goto end
...
end:
?>
might do just what you want.
Thomas

Actually no.
The problem I wanted to address was "So far the newsfeed works fine. However, JUST IN CASE the feed ever goes down, I would like the rest of the page to display properly. "
With the PHP exit() Function, nothing after the closing ">" of the PHP function will be displayed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.