PHP echo 函数返回值与函数内部的 echo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3602383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP echo function return value vs echo inside function
提问by Karl
Typically, I'll write a function like so:
通常,我会写一个像这样的函数:
function alertClass($field,$full=false){
global $formErrors;
$html = $full ? ' class="alert"' : ' alert';
if (!empty($formErrors[$field])) return $html;
}
and then where I want the html to show I'll echo the return value of the function like so:
然后在我希望 html 显示的地方我将像这样回显函数的返回值:
echo alertClass('somefield')
but today I was thinking why not just put the echo in the function instead of using it's return value? So instead of "return $html" it would be "echo $html"... Is there an advantage to one way or the other?
但今天我在想为什么不把 echo 放在函数中而不是使用它的返回值?因此,不是“return $html”,而是“echo $html”……这种方式或另一种方式有优势吗?
回答by pabenta.com.ph
for example when you echo the text out of your function just like this...
例如,当您像这样从函数中回显文本时...
function yourStatus(){
echo ' Done';
}
echo 'Status ='. yourStatus();
your output will look like this
你的输出看起来像这样
"DoneStatus ="
instead of
代替
"Status = Done"
cheers
干杯
回答by RichieHindle
Using echo
preculdes using the function to programmatically build some HTML for output later on, or for further processing.
使用echo
preculdes 使用该函数以编程方式构建一些 HTML 以供稍后输出或进一步处理。
If in your case there's no downside to returning the HTML, I'd continue doing it. It adds flexibility.
如果在您的情况下返回 HTML 没有缺点,我会继续这样做。它增加了灵活性。
回答by Artefacto
It depends on the purpose of the function.
这取决于函数的目的。
In general, you will want to have your functions as side-effect free as possible. If you go about echoing output in several places, your code will start to get very confusing. A function that returns a value is also more versatile, since the caller can decide whether to further manipulate that value or immediately echo it.
一般来说,您会希望您的函数尽可能没有副作用。如果您在多个地方回显输出,您的代码将开始变得非常混乱。返回值的函数也更通用,因为调用者可以决定是进一步操作该值还是立即回显它。
However, if the purpose of the function is specifically to output text (e.g. methods on a class responsible for building and outputting a page, according to a template), then it would fine.
但是,如果该函数的目的是专门输出文本(例如,根据模板,负责构建和输出页面的类上的方法),那就没问题了。
回答by shamittomar
If you are not using the returned value again anywhere, then directly echo
ing is better.
如果您不再在任何地方使用返回值,那么直接echo
ing 更好。
回答by KiranKumar
echo
is a PHP language construct which pushes values to the output buffer. It does not have a return value, so concatenating it with a string would cause everything after the echo
to immediately be sent to the output buffer, and everything prior to echo
to compose the concatenated string. This is such a misuse of echo
that PHP itself doesn't actually allow it - if you had WordPress debugging enabled you would see an error similar to
echo
是一种将值推送到输出缓冲区的 PHP 语言构造。它没有返回值,因此将它与字符串连接会导致 之后的所有内容echo
立即发送到输出缓冲区,而之前的所有内容都echo
将组成连接的字符串。这是对echo
PHP 本身实际上不允许的滥用- 如果您启用了 WordPress 调试,您将看到类似于以下内容的错误
Parse error: syntax error, unexpected 'echo' (T_ECHO)
解析错误:语法错误,意外的“回声”(T_ECHO)
This error is what is causing your white screen - when not in debug mode, WordPress suppresses error output to avoid exposing potentially sensitive information to end-users.
此错误是导致白屏的原因 - 不在调试模式下时,WordPress 会抑制错误输出,以避免将潜在的敏感信息暴露给最终用户。
You shouldn't use echo
in shortcode logic, as internally WordPress does more processing with a shortcode's return value. So using echo
in a shortcode has a good chance to mess up your final markup.
您不应该echo
在简码逻辑中使用,因为在内部 WordPress 会使用简码的返回值进行更多处理。因此,echo
在短代码中使用很可能会弄乱您的最终标记。
The inclusion of the echo
before the edd_get_cart_total()
does not result in currency formatting. I've dug through the plugin in question's source codejust to be sure. Rather, it's more likely that some function is hooked to the edd_get_cart_total
filter to format the output in templates (thus formatting the total when you used it in your header.php
template), however within the context of a shortcode that filter is not attached.
在echo
之前的包含edd_get_cart_total()
不会导致货币格式。为了确定起见,我已经仔细研究了有问题的插件的源代码。相反,更有可能的是某些函数与edd_get_cart_total
过滤器挂钩以格式化模板中的输出(从而在您在header.php
模板中使用它时格式化总数),但是在不附加过滤器的短代码的上下文中。
Conveniently, the plugin provides the ebb_cart_total()
function which will always produce a currency-formatted total string. The first argument to the function is $echo
which is true by default, and will cause the function to display the total instead of returning it - which, as detailed earlier, is not something you want to do in a shortcode - so set this argument to false
to have the function return a string which you may concatenate with the rest of your shortcode markup.
方便的是,该插件提供了ebb_cart_total()
始终生成货币格式的总字符串的功能。函数的第一个参数$echo
默认为真,并且会导致函数显示总数而不是返回它——如前所述,这不是你想要在短代码中做的事情——所以将此参数设置false
为让函数返回一个字符串,您可以将其与短代码标记的其余部分连接起来。
All together:
全部一起:
function eddminicartfunc() {
return
'<div class="mobilemenucart">
<i class="fa fa-shopping-cart"></i>
<span class="header-cart-total"> ' . edd_cart_total( false ) . ' </span>
<span class="header-cart edd-cart-quantity">' . edd_get_cart_quantity() . '</span>
</div>';
}
add_shortcode( 'eddminicart', 'eddminicartfunc' );