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.
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)".