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