Best way to determine if a function has an output?

I have a list of functions that perform a fairly deep procedure to determine which post_id to get its content and display it on the site interface.

When this function returns its contents, I want it to be wrapped in an html wrapper. I want this html shell to load only when the function returns the result.

In the example, I have the following ...

public static function output_*() { // my routines that check for content to output precede here // if there IS content to output the output will end in echo $output; // if there is NO content to output the output will end in return; } 

In full explanation, I have the following ...

If one of these functions returns output , I want it to be wrapped in an html wrapper, so in theory I'm trying to do something like this ...

 public static function begin_header_wrapper() { // This only returns true if an output function below returns content, // which for me is other than an empty return; include(self::$begin_header_wrapper); } public static function output_above_header() { // my routines that check for content to output precede here // if there is content to return it will end in the following statement // otherwise it will end in return; include($begin_markup); // This is the BEGIN html wrapper for this specifc output // It is, so let get this option post id number, extract its content, // run any needed filters and output our user selected content $selected_content = get_post($this_option); $extracted_content = kc_raw_content($selected_content); $content = kc_do_shortcode($extracted_content); echo $content; include($end_markup); // This is the END html wrapper for this specifc output } public static function output_header() { // the same routine as above but for the header output } public static function output_below_header() { // the same routine as above but for the below header output } public static function end_header_wrapper() { // This only returns true if an output function above returns content, // which for me is other than an empty return; include(self::$end_header_wrapper); } 

I know right now, ahead of time I don’t want to determine twice (once at the beginning and once at the end) if one of the output functions has an output, when there should be a way to do this with one check, but I would like to start working on this rabbit hole and figure out the best way to determine if my function returns something or not.

Or, if there is a completely better way to approach this, go through everything, lol and let me know.

I watched this article online and others @ Find out if there is a function with php output

So in the end, I just wanted to find out if there is a better way to approach this and what is actually the BEST way you think to check if my functions have output to return, so I can run my html shell based on these conditions?

Would it be better ob_get_length ? When I looked at the ob targets, this one seemed the best and most simplified, but I wanted to get advice, feedback. Or maybe I can check if my $content variable is being returned? Thank you I really appreciate it!

+7
php output wordpress wordpress-theming
source share
1 answer

You can catch the result and save it in a variable, which is then passed to the empty () function.

 if(!empty(($output = yourFunctionToTest(param1, paramN)))) { // do something with $output (in this case there is some output // which isn't considered "empty" } 

Performs your function, stores the result in a variable ($ output in this case), and performs an empty () check of the contents of the variables. After that, you can use the contents of $ output.

Remember that empty () treats empty strings or 0 as "empty", therefore returns true .

Alternatively, you can use functions such as isset () to determine if the variable is null .

http://php.net/isset

http://php.net/empty

+1
source share

All Articles