PHP echo函数返回值与内部函数echo [英] PHP echo function return value vs echo inside function
本文介绍了PHP echo函数返回值与内部函数echo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,我将这样编写一个函数:
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;
}
,然后在希望HTML显示的位置回显该函数的返回值,如下所示:
and then where I want the html to show I'll echo the return value of the function like so:
echo alertClass('somefield')
但是今天我在想为什么不只将echo放入函数中而不使用返回值呢? 因此,它不是返回$ html",而是"echo $ html" ... 一种或另一种方式有优势吗?
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?
推荐答案
例如,当您像这样将文本从函数中回显时...
for example when you echo the text out of your function just like this...
function yourStatus(){
echo ' Done';
}
echo 'Status ='. yourStatus();
您的输出将如下所示
"DoneStatus ="
代替
"Status = Done"
欢呼
这篇关于PHP echo函数返回值与内部函数echo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文