开发者

PHP - How can I check if return() was called from an include()'d file?

How can I tell if return() was called from within the included file. The problem is that include() returns 'int 1', even if return() wasn't called. Here is an example...

included_file_1.php

<?php

return 1;

included_file_2.php

<?php

echo 'no return here, meep';

main.php

<?php

$ret = include('included_file_1.php');

// This file D开发者_开发百科ID return a value, int 1, but include() returns this value even if return() wasn't called in the included file.
if ($ret === 1)
{
    echo 'file did not return anything';
}

var_dump($ret);

$ret = include('included_file_2.php');

// The included file DID NOT return a value, but include() returns 'int 1'
if ($ret === 1)
{
    echo 'file did not return anything';
}

var_dump($ret);

Edit: As a temporary solution, I'm catching the output from include() (from echo/print statements). If any output was produced, I ignore the return value from include().

Not pretty, but it provides the functionality I need in my web app/framework.


Good question. Looking at the examples in the manual, I guess you can't.

The best workaround idea that comes to mind is to make all your includes in your project not return naked values, but an array containing the return value (return array(1);).

Everything else that comes to mind (mine at least) involves defining some constants or variables to flag whether you're in an include or not. Nothing elegant.


This behavior is by design - the include statement returns 1 when the include is successful (or the value return'ed by the included file), as script execution can continue even if an include fails and a programmer might what to check for this case.

From the manual:

[the return value] is 1 because the include was successful. Notice the difference between the above examples. The first uses return() within the included file while the other does not. If the file can't be included, FALSE is returned and E_WARNING is issued.

In this case, simply don't use 1 in your code as a return value for a file, i.e., this return value is effectively reserved for use by the PHP include statement.


You could probably make use of debug_backtrace(), but it's not really an ideal solution. It's slow, and you'd have to do a lot of work to get the results you wanted.

Unfortunately, the situation you describe falls outside the regular usage of include and return. I would simply alter your logic to work with it. Why not have files that usually don't call return at the end simply call return 2? That way you would get a value that still evaluates to true on normal comparisons, but you could use it as a distinction between the other return 1 calls.


If there is nothing to return in the included file, then the include function returns 1 by default. Otherwise it will return whatever is sent by the included file. Echo and print statements are not a return, it has to be the 'return' statement designated by PHP. Everything else will be processed normally. This code is basically saying, 'did the file execute properly?' and it's returning 1, 'yes it did.'

PHP: include - Manual - Take a look at "Handling Returns" right below the security warning messages and then look at Example 5 and the text right below it. It explains it all perfectly.


$ret = include('...'); Will always return 1 if the file exists.

Try creating a variable and check if it is set.

include_file_1.php

<?php
$includeFile1 = true;

main.php

<?php
include('included_file_1.php');

if (isset($includeFile1) == true) 
{
    echo "It was included";
}


Never needed to, and never knew you could, but I might use this at the end of each included script:

return __FILE__; 

or:

return basename(__FILE__);

or if you don't need that specificity, use 2 as suggested earlier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜