php - How to assign the echo statement from a function call to a variable? -
i trying echo of function , add value variable.
so i've done.
function myfunction() { echo 'myvar'; }
then want variable this:
$myvariable = myfunction();
i thought work isn't working.
is not way this? if not, how do this?
you can call function, while have output buffering turned on , can capture output. e.g.
<?php ob_start(); myfunction(); $variable = ob_get_contents(); ob_end_clean(); echo $variable; ?>
output:
myvar
or change echo
statement return
statement.
your current version doesn't work, because don't return value function. , since omitted return statement value null gets returned , assign variable. can see doingthis:
$variable = myfunction(); var_dump($variable);
output:
null
Comments
Post a Comment