[RESOLVED] Need an explanation of 'exit' keyword

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

[RESOLVED] Need an explanation of 'exit' keyword

Post by vaughtg »

Greetings all,
I've got a main php page that has the following code:

Code: Select all

        <div id="bothrcontent">
          <?PHP
            // get navigation based on page called by URL
           if(in_array($needle, $haystack)){
             $contentfile = $needle.".php";
             require $contentfile;
           }
          ?>
      	</div>
And, under certain circumstances, this is what is encountered in $contentfile:

Code: Select all

function error($msg) {
    ?>
    <script type="JavaScript">
		<!--
        alert('<?=$msg?>');
        history.back();
    //-->
    </script>
    <?
    exit;
}
The problem I'm encountering is that when the exit; is encountered it stops ALL php/html processing - so the last thing that is sent to the browser is the closing </script> tag. I'd like to understand what is going on with the exit command AND find an alternative that will return back to the wrapper/parent page and allow it to continue processing to the browser.
Last edited by vaughtg on Thu Apr 12, 2012 8:19 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need an explanation of 'exit' keyword

Post by requinix »

exit() does exactly what you said it does: stops all processing. There's not much else to understand.
vaughtg wrote:find an alternative that will return back to the wrapper/parent page and allow it to continue processing to the browser.
There's your hint.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: Need an explanation of 'exit' keyword

Post by vaughtg »

First off, I'm not looking for hints. Knowledge shared would be nice.

Next, return didn't do anything different than exit - no code after that block processes. Including no code from the calling/"requiring" page this file was supposed to be wrapped inside.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: Need an explanation of 'exit' keyword

Post by vaughtg »

Another issue with using return is that it just sends the processing back to the script that called the function - it does not kick out of the current file and back to the requiring file.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need an explanation of 'exit' keyword

Post by requinix »

I was being cute. "return" is the keyword you need to use: it stops processing of only the current file.

As for your claim, there's something else going on. I know because I use return; all the time.

Code: Select all

<?php // a.php

include "b.php";
echo "In a.php";

?>

<?php // b.php

echo "In b.php";
return;
echo "Still in b.php";

?>

Code: Select all

In b.php
In a.php
vaughtg wrote:Another issue with using return is that it just sends the processing back to the script that called the function - it does not kick out of the current file and back to the requiring file.
If you use it in a function, yes. In the top-most scope of a file (ie, not a function) it stops the file. I did leave that "top-most scope" out because I thought that was already the case here.
vaughtg
Forum Commoner
Posts: 39
Joined: Thu Feb 17, 2011 9:43 am

Re: Need an explanation of 'exit' keyword

Post by vaughtg »

Wow, I thought this thread was older than it is. Anyway, thanks for the help. I'm gonna mark this one resolved. Got it going with this information.
Post Reply