Best way to determine if a function has an output?

Fearless Mode

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!

GxTruth

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best way to determine that a notification has been removed or dismissed by the user?

From Dev

What is the best way to find an input for a function if you already know the output?

From Dev

Best way to output Javascript?

From Dev

Is it possible to determine if function has arguments?

From Java

In Java, what is the best way to determine the size of an object?

From Dev

Best way to determine if a user went offline in MySQL

From Dev

Best way to determine user's language

From Dev

SQL - Best way to determine Operation Type in Trigger?

From Dev

git: Best way to determine unmerged branches in a script

From Dev

Best way to determine to current foreground application, Android

From Dev

SQL - Best way to determine Operation Type in Trigger?

From Dev

Best way to determine if simple or extended MAPI was loaded

From Dev

Best way to determine if a user went offline in MySQL

From Dev

git: Best way to determine unmerged branches in a script

From Dev

Best way to determine the type of an object at runtime

From Dev

Correct Way to determine if function is set

From Dev

Determine function has defined as a expression or declaried as function

From Dev

Best way to determine which time is the best to push into an array?

From Dev

Reverse the way of function output

From Dev

Is there a way to determine whether a parameter has this modifier?

From Dev

Proper way to determine if a Map has certain keys

From Dev

Is there a way to determine if the curent user has delete righte?

From Dev

php way to determine if apache has connections waiting

From Dev

Best way to return a function value and call another method after async animation block or timer has ended

From Dev

What's the best way to pass an output `io::Write` to a function that uses threads?

From Dev

Best way to pass function as parameter

From Dev

What is the best way to invoke a function?

From Dev

The best way to implement the strcpy function

From Dev

Best way to call a function in javascript

Related Related

  1. 1

    What is the best way to determine that a notification has been removed or dismissed by the user?

  2. 2

    What is the best way to find an input for a function if you already know the output?

  3. 3

    Best way to output Javascript?

  4. 4

    Is it possible to determine if function has arguments?

  5. 5

    In Java, what is the best way to determine the size of an object?

  6. 6

    Best way to determine if a user went offline in MySQL

  7. 7

    Best way to determine user's language

  8. 8

    SQL - Best way to determine Operation Type in Trigger?

  9. 9

    git: Best way to determine unmerged branches in a script

  10. 10

    Best way to determine to current foreground application, Android

  11. 11

    SQL - Best way to determine Operation Type in Trigger?

  12. 12

    Best way to determine if simple or extended MAPI was loaded

  13. 13

    Best way to determine if a user went offline in MySQL

  14. 14

    git: Best way to determine unmerged branches in a script

  15. 15

    Best way to determine the type of an object at runtime

  16. 16

    Correct Way to determine if function is set

  17. 17

    Determine function has defined as a expression or declaried as function

  18. 18

    Best way to determine which time is the best to push into an array?

  19. 19

    Reverse the way of function output

  20. 20

    Is there a way to determine whether a parameter has this modifier?

  21. 21

    Proper way to determine if a Map has certain keys

  22. 22

    Is there a way to determine if the curent user has delete righte?

  23. 23

    php way to determine if apache has connections waiting

  24. 24

    Best way to return a function value and call another method after async animation block or timer has ended

  25. 25

    What's the best way to pass an output `io::Write` to a function that uses threads?

  26. 26

    Best way to pass function as parameter

  27. 27

    What is the best way to invoke a function?

  28. 28

    The best way to implement the strcpy function

  29. 29

    Best way to call a function in javascript

HotTag

Archive