views:

84

answers:

2

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?

+4  A: 

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

halfdan
+3  A: 

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.

Emil H
This is an interesting method, and if I could give the right answer to both of you, I would, because using @halfdan's method in conjunction with yours got me *exactly* what I needed.Thanks!
gms8994