Reputation: 12320
We have a custom framework that we use, that allows us to include
code in to other pages. However, the header for this code is always printed, even if the include
code doesn't output anything. Is there a way that I can exit
, or return
something specific, that I can trap and catch via include
/require
, so that I can do the right thing?
Upvotes: 2
Views: 803
Reputation: 40240
This might not be the most suitable solution, but it's a nice trick. You can actually use PHP-includes as functions and return values.
For example. Create a file called myinclude.php:
<?php
$a = 9192*9192;
return a;
Then create a second file and include the first:
<?php
$res = include("myinclude.php");
echo $res;
Yes, it actually works. See the manual page for more details. Also note that if nothing is explicitly returned from the include file, include() will return "OK" (oddly enough). This works for require(), include_once() and require_once() as well.
Upvotes: 3
Reputation: 34214
Activate the output buffer, include the file and then check for its content:
ob_start();
include "file.php";
$content = ob_get_contents();
ob_end_clean();
After that check for the content generated and either keep it or dump it.
Best wishes, Fabian
Upvotes: 5