Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to determine if a function has an output?

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!

like image 288
Fearless Mode Avatar asked Aug 23 '16 21:08

Fearless Mode


People also ask

How do you find the output of a function?

In simple terms, the input is what goes into the function and the output is what comes out of the function. In the function y = x + 5 y = x + 5 , the x is the input variable and the y is the output variable. The function works by taking an input value, x = 3 for example, and producing an output value.

How do you know if the input and output is a function?

When each input value has one and only one output value, that relation is a function. Functions can be written as ordered pairs, tables, or graphs.

What is an output function?

An output function is a function that an optimization function calls at each iteration of its algorithm. Typically, you use an output function to generate graphical output, record the history of the data the algorithm generates, or halt the algorithm based on the data at the current iteration.


1 Answers

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

like image 118
GxTruth Avatar answered Oct 21 '22 09:10

GxTruth