Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Check If require_once() Content Is Empty

I am working on a PHP script which involves me including several external PHP scripts via the "require_once()" method. I would like to know if there is a way for the master script (the one including the others) to tell whether or not the processed output from included script has generated any content.

So, for example, maybe the user's permissions for a particular script results in PHP not generating any output. So, maybe, the master script would echo something like:

                             Nothing interesting here!

Is there a way to do that in the master script, or would I need to create these tests inside of the included script, and return the results to the master script?

Thank you for your time,
spryno724

like image 330
Oliver Spryn Avatar asked Dec 21 '22 15:12

Oliver Spryn


2 Answers

You can capture the output using ob_start, ob_get_contents and ob_end_clean like this:

ob_start();
require_once('script.php');
$output = ob_get_contents();
ob_end_clean();
like image 66
Mark Elliot Avatar answered Jan 06 '23 10:01

Mark Elliot


ob_start();
require_once 'your_file.php';
$output = ob_get_flush(); // ob_get_clean() if you want to suppress the output

if(empty($output)) {
    echo 'Nothing interesting here!';
}
like image 42
John Flatness Avatar answered Jan 06 '23 11:01

John Flatness