get return from echo
Asked Answered
N

5

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'
Nickerson answered 23/9, 2011 at 14:18 Comment(7)
There is, use the search. By the way, using functions which directly output any data are considered bad style.Axe
This was the only way I found so far.Corrales
@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.Buckskins
@Axe Thx / when you say 'output' are you referring to returning or echoing?Nickerson
@Buckskins 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".Axe
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();Nickerson
The performance test: dev.airve.com/demo/speed_tests/php_get_echo.phpNickerson
B
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.

Buckskins answered 23/9, 2011 at 14:25 Comment(7)
@NikiC Your edit is incorrect. The middle line needs to be $func; without the parens. Otherwise it causes errors.Nickerson
Thanks—that should save the next person who finds this from going mad with confusion. ;)Nickerson
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.Africander
@Africander $func = some_callback()Nickerson
@Nickerson 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()).Africander
@Africander 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.Nickerson
@Nickerson 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.Africander
A
6

Your first code is correct. Can be shortened though.

  function getEcho($function) {
        ob_start();
        $function;
        return ob_get_clean();
    }
    echo getEcho($function);
Abject answered 23/9, 2011 at 14:32 Comment(0)
A
2

Your first piece of code is the only way.

Aleph answered 23/9, 2011 at 14:25 Comment(0)
J
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.
Jenniejennifer answered 23/9, 2011 at 14:42 Comment(1)
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 themehybrid.com/hybrid-coreNickerson
L
0
function getEcho() {
    ob_start();
    myEcho();
    return ob_get_clean();
}
$myvar =  getEcho();
Laoighis answered 2/3, 2022 at 10:17 Comment(2)
if you call like this then function will echo in argument so this code is working for me getEcho($function)Laoighis
If you want to update your post, please edit instead of commenting.Joscelin

© 2022 - 2024 — McMap. All rights reserved.