5

I'm working with some functions that echo output. But I need their return so I can use them in PHP.

This works (seemingly without a hitch) but I wonder, is there a better way?

    function getEcho( $function ) {
        $getEcho = '';
        ob_start();
        $function;
        $getEcho = ob_get_clean();
        return $getEcho;
    }

Example:

    //some echo function
    function myEcho() {
        echo '1';
    }

    //use getEcho to store echo as variable
    $myvar = getEcho(myEcho());      // '1'
ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 2
    There is, use the search. By the way, using functions which directly output any data are considered bad style. – str Sep 23 '11 at 14:22
  • 1
    This was the only way I found so far. – Aleks G Sep 23 '11 at 14:23
  • 1
    @str: there are some built-in functions that one can't change and that output data directly (var_dump for example). but for self-written functions you're right. – oezi Sep 23 '11 at 14:31
  • @str Thx / when you say 'output' are you referring to returning or echoing? – ryanve Sep 23 '11 at 14:53
  • @oezi I know, but that should not be adapted to your own functions (just saying). ryanve: I meant echoing and "There is NOT" instead of "There is". – str Sep 23 '11 at 14:55
  • Worth noting: I just ran a performance test w/ this (using the streamlined version in @oezi's answer) and `echo return_echo(myEcho());` takes roughly 5x as long as `myEcho();` – ryanve Sep 24 '11 at 23:40
  • The performance test: http://dev.airve.com/demo/speed_tests/php_get_echo.php – ryanve Sep 25 '11 at 15:46

5 Answers5

10

no, the only way i can think of to "catch" echo-statements it to use output-buffering like you already do. i'm using a very similar function in my code:

function return_echo($func) {
    ob_start();
    $func;
    return ob_get_clean();
}

it's just 2 lines shorter and does exactly the same.

oezi
  • 51,017
  • 10
  • 98
  • 115
  • @NikiC Your edit is incorrect. The middle line needs to be `$func;` without the parens. Otherwise it causes errors. – ryanve Sep 25 '11 at 15:43
  • Thanks—that should save the next person who finds this from going mad with confusion. ;) – ryanve Sep 25 '11 at 16:00
  • Huh? But what is `$func;` supposed to do? It's just a variable lookup which by itself does nothing. `$func();` would call the function passed in. – NikiC Sep 25 '11 at 16:27
  • @NikiC `$func = some_callback()` – ryanve Sep 25 '11 at 16:45
  • 1
    @ryanve You got something wrong. `$func = some_callback()` will just store the return value from `some_callback()` in `$func`. If you want to store the function as a callback you need to write `$func = 'someCallback'` and call it using `$func`. PS: Your initial code will not work, because in `getEcho(myEcho());` `myEcho()` will just do its echo and only after that `getEcho()` will be executed. What you need to do is `getEcho('myEcho')` (combined with `$func()`). – NikiC Sep 25 '11 at 16:48
  • @NikiC You're right—good call. I'd been echoing afterwards anyway so it appeared it was working. Yea with the parens the input would have to be the function name only. I'm trying to make it work passing the callback (hence the error). A complication is the arguments. Ideally I'd be able to do `getEcho( someFunc('arg1', 'arg2', ...) );` where it works when the inner function takes any number of args. – ryanve Sep 25 '11 at 17:23
  • 1
    @ryanve In that case you would need to create a Closure around it, i.e. `getEcho(function() { someFunc('arg1', 'arg2', ...); });`. As you can see the problem get's pretty complicated, so normally the best option is to redesign your functions to just return instead of echoing directly. – NikiC Sep 25 '11 at 17:26
6

Your first code is correct. Can be shortened though.

  function getEcho($function) {
        ob_start();
        $function;
        return ob_get_clean();
    }
    echo getEcho($function);
Mob
  • 10,958
  • 6
  • 41
  • 58
2

Your first piece of code is the only way.

Yeroon
  • 3,223
  • 2
  • 22
  • 29
1

Did you write these functions? You can go 3 ways:

  1. Using your wrapper to do capturing via output buffering.
  2. Extra set of functions calls, wordpress style, so that "somefunc()" does direct output, and "get_somefunc()" returns the output instead
  3. Add an extra parameter to the functions to signal if they should output or return, much like print_r()'s flag.
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • The functions that I need to use this for are ones I didn't write—they're either ones from WordPress that don't have a get_ version or ones from http://themehybrid.com/hybrid-core – ryanve Sep 23 '11 at 14:52
0
function getEcho() {
    ob_start();
    myEcho();
    return ob_get_clean();
}
$myvar =  getEcho();
Rahul
  • 1
  • 1