Echo/Return Logic

A lot of PHP functions need to return or echo, depending on what parameters are passed. In WordPress, the $echo parameter, or query string parameter, is usually employed. At the end of the function, $echo is checked, which determines echo vs return. Because this is done in so many functions, there might as well be a standard way of doing things. I’ve seen all sorts of methods in WP.

Example 1:

if ( $echo ) {
  echo $return;
} else {
  return $return;
}

Example 2:

if ( $echo ) {
  echo $return;
}
return $return;

But my personal favorite is the following:

if ( !$echo )
  return $return;
echo $return;

It is only three lines and it doesn’t have example 2’s problem of echoing and then returning needlessly. If it is a return situation, the code stops executing at that point, so there is no need for an else block.

It’s all about the details, people.

4 thoughts on “Echo/Return Logic

  1. I can think of situations where it might be reasonable to expect that a function that echoes would also return the value that it outputs. There are a few cases of functions without an $echo argument that echo by default that would be improved by returning that value as well.

    I like example #2 for another reason. It allows the entire function to execute to the return as the last function statement, rather than short-circuiting the code path with a return before any following lines. It would be an easy novice mistake to assume that lines following the return in your last example (outside of the conditional) would execute, and that you could add new lines there (before the echo) to expand the functionality of the function.

    I think there are cases where some of these constructions might be more useful than others. In the case of some functions, the value echoed by the function could be different from the value returned. I can’t recall exactly which (the_title()?), but there is one function whose text output is useful but embroiled in enough HTML that it’s not possible to simply call it when you want only the text. If your last construction was used, it would be more difficult to return a value that was different from what it echoed. The potential confusion caused by returning and echoing different values leads me to a more significant point.

    I would posit that no function that generates values (in WordPress) should echo. Either code should echo the results of a function, or there should be a wrapper function that echoes (mostly for the purpose of use in templates). Any function that uses an echo parameter should deprecate that parameter in favor of adding a “template tag”-style function (the_whatever()) or be otherwise re-engineered to return a useful value to a wrapper function that can echo.

    If this was standardized, it would be possible to better abstract the business logic from the presentation layer – a commonly accepted good practice – and the standardization would be on enforcing good logic rather than producing code to a specific template of syntactical structure.

  2. return wp_return($value, $echo);
    }

    function wp_return($value, $echo) {
    echo $echo ? $value : ”;
    return $value;
    }

    😛

Comments are closed.