PHP Check If require_once() Content Is Empty
Asked Answered
Z

4

5

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

Zemstvo answered 6/4, 2011 at 2:12 Comment(1)
You will first have to process the script, to check if return is empty.Immunochemistry
L
9

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();
Lycaonia answered 6/4, 2011 at 2:15 Comment(0)
S
4
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!';
}
Saddlebow answered 6/4, 2011 at 2:15 Comment(0)
Q
0

Do not worry if the require_once is executed on not. Try to identify if the variables/functions of require_once files are working or not by using the debug function like this:-

var_dump($Your_Variable_Name_from_require_Once_file);
Quadriceps answered 29/7, 2017 at 3:8 Comment(0)
S
0

If you like you can also use:

@$result = include $filename;

include does return false when the file can't be found, but it does also generate a warning. That's why you need the @.

I was tempted to use file_exists() however as noted in a comment in php docs, file_exists() DOESN'T SEARCH THE PHP INCLUDE_PATH and the results of this function are cached.

So if you decided to use file_exists() then you will want to probably use clearstatcache() that being the case you may want to have a look here:

https://www.php.net/manual/en/function.clearstatcache.php#125163

and here :

clearstatcache + include_path + sessions

Satchel answered 6/5, 2022 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.