if语句中的布尔条件
I want to double check something about multiple conditions in a boolean if statement in PHP. Given that I write:
if(!empty($_POST) && !isset($_POST['report_btn'])) {...}
can this be re-written as
if(! ( empty($_POST) && isset($_POST['report_btn']) ) ) {...}
I'm wanting to remove one of the exclamation marks to see if I can make the code a bit tidier. The logic is to see if I can execute the code if POST data has been sent to a page but the 'report_btn'
value is not set.
我想在PHP中的布尔if语句中仔细检查一些有关多个条件的内容。 鉴于我写了: p>
if(!empty($ _ POST)&&!isset($ _ POST ['report_btn'])){...}
code> pre>
可以重写为 p>
if(!(empty($ _ POST)&& isset ($ _POST ['report_btn']))){...}
code> pre>
我想要删除其中一个惊叹号,看看能否制作 代码有点整洁。 如果POST数据已发送到页面但未设置'report_btn' code>值,则逻辑是查看是否可以执行代码。 p>
div>
No, You can not write the above condition as below condition:
Reason:
for:
if(! ( empty($_POST) && isset($_POST['report_btn']) ) ) {...}
the result matrix will:
first_statement second_statement result
true true false
true false true
false true true
false false true
And for the below
if(!empty($_POST) && !isset($_POST['report_btn'])) {...}
The result matrix will be:
first_statement second_statement result
true true false
true false false
false true false
false false true
It means both code are not similar.
Boolean logic says that
!A && !B == !(A || B)
!A || !B == !(A && B)
It's better to know how to transform your logical expression properly.
It's good to read about De Morgan's laws which includes your case.
"not (A and B)" is the same as "(not A) or (not B)"
"not (A or B)" is the same as "(not A) and (not B)".