问题描述
当使用 isset
编码时,我收到一个致命错误.我已经搜索过 stackoverflow 但结果并不令人满意.
When coding with isset
i am getting an fatal error.I have searched stackoverflow but results are not satisfactory.
我得到了
致命错误:不能对表达式的结果使用 isset()(您可以使用null !== expression"代替)
我的代码是
if (!isset( $size || $color )) {
$style = '';
}else{
$style = 'font-size : ' . $size . ';color:' . $color;
}
推荐答案
如评论(和错误消息)中所述,您不能将表达式的结果传递给 isset
.
As mentioned in the comments (and the error message), you cannot pass the result of an expression to isset
.
您可以使用多个isset调用,或者反转if/else块的逻辑并将多个参数传递给isset,我认为这是最干净的解决方案:
You can use multiple isset calls, or reverse the logic of your if/else block and pass multiple parameters to isset, which i think is the cleanest solution:
//true if both are set
if(isset($size, $color)) {
$style = 'font-size : ' . $size . ';color:' . $color;
}else{
$style = '';
}
您可以通过首先设置默认值来进一步清理它,从而避免需要 else 部分:
You can clean this up a little further by setting the default value first, thus avoiding the need for an else section:
$style = '';
if(isset($size, $color)) {
$style = 'font-size : ' . $size . ';color:' . $color;
}
您甚至可以使用三元,但有些人发现它们更难阅读:
You could even use a ternary, though some people find them harder to read:
$style = isset($size, $color) ? 'font-size : ' . $size . ';color:' . $color : '';
这篇关于致命错误:无法对表达式的结果使用 isset()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!