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

Posted by John Himmelman on Stack Overflow See other posts from Stack Overflow or by John Himmelman
Published on 2010-03-29T17:50:04Z Indexed on 2010/03/29 17:53 UTC
Read the original article Hit count: 194

Filed under:
|

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 DID 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);

© Stack Overflow or respective owner

Related posts about php

Related posts about include