简体   繁体   English

PHP多重逻辑运算符

[英]PHP Multiple Logical Operator

Trying to figure out the best way to do this. 试图找出执行此操作的最佳方法。 I have a PHP function that checks if something is true or not, and if it is, returns some JSON data. 我有一个PHP函数,用于检查某些东西是否正确,如果是,则返回一些JSON数据。

The issue I'm having is that either one can return the data if the other is null, or both are true. 我遇到的问题是,如果另一个为null,或者两者都为true,则任何一个都可以返回数据。

This is what I've tried: 这是我尝试过的:

if( $logic_type === true && $description== 'null' ){
    return json_encode( self::returnDataArray( $return_arrs ) );
}

if( $description === true && $logic_type == 'null' ){
    return json_encode( self::returnDataArray( $return_arrs ) );
}

if($description=== true && $logic_type === true ){
    return json_encode( self::returnDataArray( $return_arrs ) );
}

That seems to be way too long in the tooth. 这似乎太久了。 So I tried: 所以我尝试了:

if( $logic_type === true && $description== 'null' || $description === true && $logic_type == 'null' || $description=== true && $logic_type === true ){
    return json_encode( self::returnDataArray( $return_arrs ) );
}

But that only works if the last condition is met. 但这仅在满足最后一个条件的情况下才有效。

You should use parentheses for grouping your conditions 您应该使用括号将条件分组

Quote from Operator Precedence 引用操作员优先

Use of parentheses, even when not strictly necessary, can often increase readability of the code by making grouping explicit rather than relying on the implicit operator precedence and associativity. 即使不是绝对必要,使用括号也可以通过使分组显式而不是依赖于隐式运算符优先级和关联性来提高代码的可读性。

Try 尝试

if( ($logic_type === true && $description== 'null') || ($description === true && $logic_type == 'null') || ($description=== true && $logic_type === true) ){

    return json_encode( self::returnDataArray( $return_arrs ) );

}

You have to enclose $logic_type === true && $description== 'null' ||.... in the brackets because you are using && and || 您必须将$logic_type === true && $description== 'null' ||....括在方括号中,因为您正在使用&&|| . Use the code below 使用下面的代码

if( ($logic_type === true && $description== 'null') || ($description === true && $logic_type == 'null') || ($description=== true && $logic_type === true ))
{
    return json_encode( self::returnDataArray( $return_arrs ) );
}

This looks like the perfect example of using an exculsive or ( A XOR B ). 这看起来像是使用排他性或A XOR B )的完美示例。 Which means either A or B , but not both. 这意味着AB ,但不是两者都。 In your case you want either $logic_type to be null, or $description to be null, but not both to be null. 在您的情况下,您希望$logic_type为空,或者$description为空,但不希望两者都为空。 That or they both would be true. 那或者它们都是真实的。 So we can simplify the expression to this, using a bit-wise XOR: 因此,我们可以使用按位XOR简化表达式:

if( ($logic_type === 'null' ^ $description === 'null') || 
    ($logic_type === true && $description === true) )
{
    return json_encode( self::returnDataArray( $return_arrs ) );
}

Note that will only be if the only possible values for $logic_type and $description where either: true or 'null' . 请注意,只有在$logic_type$description的唯一可能值为true'null'该属性才会出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM