通常,我会编写以下类似的函数:
function alertClass($field,$full=false){
global $formErrors;
$html = $full ? ' class="alert"' : ' alert';
if (!empty($formErrors[$field])) return $html;
}
然后在我想要显示HTML的地方,我将像这样回显函数的返回值:
echo alertClass('somefield')
但是今天我在想,为什么不直接把echo放到函数里面,而不是使用它的返回值呢?所以,代替 "return $html",可以使用 "echo $html" …… 两种方法之间有什么优劣吗?