小编Kar*_*arl的帖子

PHP echo函数返回值vs echo函数内部

通常,我会写一个这样的函数:

function alertClass($field,$full=false){
 global $formErrors;
 $html = $full ? ' class="alert"' : ' alert';
 if (!empty($formErrors[$field])) return $html;
}
Run Code Online (Sandbox Code Playgroud)

然后我希望html显示我将回显函数的返回值,如下所示:

echo alertClass('somefield')
Run Code Online (Sandbox Code Playgroud)

但今天我在想为什么不把回声放在函数中而不是使用它的返回值?因此,不是"返回$ html",而是"echo $ html"......对于这种或那种方式是否有优势?

php

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

php ×1