7

I have a list of functions that runs a fairly deep routine to determine which post_id to get its content from and output it on the site's frontend.

When this function returns its content I want it to be wrapped in an html wrapper. I want this html wrapper to only load if the function has an output to return.

In 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 an output I want it to be wrapped in an html wrapper, so in theory something like this is what I am trying to accomplish...

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's get this option's post id number, extract its content,
  //  run any needed filters and output our user's 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 in the start 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 get started down this rabbit hole and figure out the best way to determine if my function is returning something or not.

Or if there is an entirely better way to approach this please go all out, lol and let me know.

I looked online at this article and others @ Find out if function has any output with php

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

Would ob_get_length be the best way? As I looked over the ob purposes, this one seemed best, and most simplistic, but wanted to get some advice, feedback. Or maybe I can check if my variable $content is returned? Thanks. Really appreciate it!

Community
  • 1
  • 1
Fearless Mode
  • 241
  • 3
  • 13
  • 1
    As I wrote this I can already see that maybe the best way to run the check less is to combine my output functions into one output_header function. Then I can check once, against that one function instead of against 3 different functions. But I still would like to know about the best way to check against the function returning and output or not. - Thanks! Appreciate it. – Fearless Mode Aug 23 '16 at 21:07
  • 2
    That linked Q&A is probably what you want to do. Just capture the output of the functions and check if it is empty or not. – Rizier123 Aug 23 '16 at 23:16
  • Thanks. 2 things. I was wondering if I could just check if my variable $content is returned, because if it is not, then obviously there is no content to output so I run my html wrapper based on that. Is that possible? Alternatively with what you said, can output buffering be turned on outside of a function within a class? This way I could turn it on before my three header outputs and turn it off after, instead of turning it on and off within each function. I looked around and don't see anything that says or shows whether this can be or specifically can be done withing a class? @Rizier123 – Fearless Mode Aug 23 '16 at 23:24
  • No, you can't check just for the return value, since if there is output you won't be able to catch it anymore. Yes you can just start the output buffering in your class inside a method, call your function(s), and then check if you captured something or not. – Rizier123 Aug 23 '16 at 23:28
  • @Rizier123 Ok, thanks, I understand. I'm still new to php and while I understand the concept of the linked article, I get lost when it comes to the way it is written, the $f, the $p, the need for the integer? Is it not written plain to me? Could you provide a more understandable example for me? – Fearless Mode Aug 23 '16 at 23:33
  • Yes of course. So in the linked example they put the output buffering to capture a potential output of a function inside a function itself. So `$f` is the function name, for example `output_header`. Then you might have parameters with which you call the function. So you pass an array of parameters where each parameter is one array element, this is `$p`, for exmaple `$p = array("param 1", "param 2");`. Then they use `call_user_func_array` to call your passed function with the passed parameters. – Rizier123 Aug 23 '16 at 23:34
  • So e.g. `$f = "output_header"`, `$p = array(1, 2);` would then translate into: `output_header(1, 2);`. And at the end they check if they captured some output in `$s` and return a boolean depending on if the function outputted something or not: `return (bool)($s !== '');`. – Rizier123 Aug 23 '16 at 23:34
  • 1
    @Rizier123 GREAT!!! Thanks! I understand that. I didn't know the $f = function_name and $p = params. That helps a lot, haha. And now makes sense. Thanks! If you think it is valid to make your comment an answer I can mark this as answered. Thanks! – Fearless Mode Aug 23 '16 at 23:57

1 Answers1

1

You can catch the result and store it in a variable, which is then given 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"
}

This executes your function, stores the output in a variable ($output in this case) and executes empty() to check the variables content. You are able to use the content of $output afterwards.

Be aware that empty() consideres empty strings or 0 as "empty", therefore returning true.

As an alternative you may use functions like isset() to determine if a variable is not null.

http://php.net/isset

http://php.net/empty

GxTruth
  • 509
  • 4
  • 9
  • Awesome! Thanks for this. Tested, works great. Simple, light too :) I wrote an example comparing the TRUE vs. FALSE in a gist @ https://gist.github.com/NoahjChampion/c83bfcdd6270397261ac5b7458d797cc - For myself and feel free to update your answer with that code if you want. Either way, thanks! :) Appreciate it! @GxTruth – Fearless Mode Mar 01 '17 at 17:54