Re: using return in include files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Aaron Axelsen wrote:
I'm trying to figure out what the desired behavior is of using the
return function to bail out of an include page.

I did some testing, and this is what I concluded.

First, I created the following file:

<?php
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";
?>

I then called it as follows:
include('test.php');
include('test.php');
include('test.php');

The output is:
blah blah blah blah

Second, I changed the test.php file to be the following:

<?php
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";

function myFunc($test) {

}
?>

When I load the page now, it throws the following error: PHP Fatal
error: Cannot redeclare myfunc()

It appears that if there are functions in the include page that you
can't use return to bail out.  What is the desired functionality in this
case?  Is this a bug in how php handles it? or was return never designed
to be used this way?

It's not a bug.

When you include a file, the whole file is loaded in to memory.

Then php decides (based on your code decisions) about what to do with all of that code.


See http://www.php.net/include, specifically:

If there are functions defined in the included file, they can be used in the main file independent if they are before return() or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn't complain about functions defined after return().

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux