通常,我会写一个这样的函数:
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 ×1