致命错误:无法对表达式的结果使用isset() [英] Fatal error: Cannot use isset() on the result of an expression
问题描述
使用 isset
进行编码时,我遇到了一个致命错误。我搜索了stackoverflow,但结果并不令人满意。
我得到
致命错误:对表达式的结果不能使用isset()(可以使用null!==我的代码是
if(!isset($ size || $ color)){
$ style ='';
} else {
$ style ='font-size:'。 $尺寸。 ';颜色:'。 $颜色;
}
解决方案和错误信息),你不能将表达式的结果传递给
isset
。
你可以使用多个isset调用或反转if / else块的逻辑并将多个参数传递给isset,我认为这是最干净的解决方案:
//如果两者都设置为
,则返回true(isset($ size,$ color)){
$ style ='font-size:'。 $尺寸。 ';颜色:'。 $颜色;
} else {
$ style ='';
$ / code>您可以通过先设置默认值来进一步清理它,因此避免需要其他部分:
$ style ='';
if(isset($ size,$ color)){
$ style ='font-size:'。 $尺寸。 ';颜色:'。 $颜色;
$ / code>甚至可以使用三元组,但有些人发现它们难以阅读:
$ style = isset($ size,$ color)? '字体大小 : ' 。 $尺寸。 ';颜色:'。 $ color:'';
When coding with
isset
i am getting an fatal error.I have searched stackoverflow but results are not satisfactory.I am getting
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
My codes are
if (!isset( $size || $color )) { $style = ''; }else{ $style = 'font-size : ' . $size . ';color:' . $color; }
解决方案As mentioned in the comments (and the error message), you cannot pass the result of an expression to
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 = ''; }
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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!