php 致命错误:无法对表达式的结果使用 isset()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39040936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:47:56  来源:igfitidea点击:

Fatal error: Cannot use isset() on the result of an expression

phphtmlwordpress

提问by Musa Muaz

When coding with isseti am getting an fatal error.I have searched stackoverflow but results are not satisfactory.

isset我一起编码时遇到致命错误。我搜索了 stackoverflow,但结果并不令人满意。

I am getting

我正进入(状态

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

致命错误:不能对表达式的结果使用 isset()(您可以改用“null !== expression”)

My codes are

我的代码是

if (!isset( $size || $color )) {
    $style = '';    
}else{
    $style = 'font-size : ' . $size . ';color:' . $color;   
}

回答by Steve

As mentioned in the comments (and the error message), you cannot pass the result of an expression to isset.

正如评论(和错误消息)中所述,您不能将表达式的结果传递给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:

您可以使用多个isset调用,或者反转if/else块的逻辑并将多个参数传递给isset,我认为这是最干净的解决方案:

//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:

您可以通过首先设置默认值来进一步清理它,从而避免需要 else 部分:

$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 : '';

回答by scaisEdge

you should use this way

你应该用这种方式

if  (!isset( $size ) || !isset( $color ))  {

回答by KmasterYC

Your expression always return either true or false => In theory isset always return true so PHP not allow this
Change

您的表达式总是返回 true 或 false => 理论上 isset 总是返回 true 所以 PHP 不允许这个
更改

if (!isset( $size || $color )) {

To

if (!isset($size) || !isset($color)) {