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?


10 Years
Discussion Span
Last Post by Thomas.Fischer

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.


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

I hope this helps,



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';

//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
might do just what you want.


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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.