H
H
Hikky2021-06-21 14:41:44
PHP
Hikky, 2021-06-21 14:41:44

How to write the response of a function to a variable?

I have a function that cannot be changed, when called, I get an echo in response. Can I somehow write this answer into a variable beforehand, and only then display it? Now the result is not written to the variable, but is displayed immediately.

Example (conditional!):

function test() {
    //...
    $a = 100;
    echo $a;
}
$b = test();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-06-21
@Hikky

Use buffering:

ob_start();
test();
$b = ob_get_clean();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question