开发者

Negate echo statement and convert to value? [duplicate]

开发者 https://www.devze.com 2023-03-28 02:34 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: capturing echo into a variable
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

capturing echo into a variable

I have a function that echos a string. Is their any way (besides altering the function) that I can negate the echo statement?

example:

function echo_string(){
  echo "This is a string";
}

$dont_echo = negate_echo(echo_string());

$dont_echo .= ", 开发者_JS百科a negated string";

echo $dont_echo; // "This is a string, a negated string";

Is their anything like negate_echo?


ob_start();
echo_string();
$buffer = ob_get_clean();

That is: Starting a new output buffer, execute the function, get the buffers content and close it. See ob_get_cleanDocs.


The only way is to use output buffering:

ob_start();
echo_string();
$output = ob_get_clean();

echo $output.", a negated string";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号