if($a=="" and $b=="" or $c=="") {
echo "something";
}
I want to ask that if $a
AND $b
is empty then it will print: something
. But if $b
is not empty, then it will check the statement like this e.g. if $a
is empty AND $c
is empty then print: something
(Means $a
is compulsory to check with the variable which is empty $b
OR $c
)
Just force the comparison order/precedence by using parentheses:
if( $a == "" and ( $b == "" or $c == "" ) )
But you would be better off using &&
and ||
:
if( $a == "" && ( $b == "" || $c == "" ) )
I suppose you need something like this:
if (
($a == '')
&&
(
($b == '')
||
($c == '')
)
)
so, it will print something
only when $a is empty and either $b or $c empty
See the PHP Operator Precedence table. and
has higher precedence than or
, so your condition is treated as if you'd written
if (($a == "" and $b == "") or ($c == ""))
Since you want the $a
check to be independent, you need to use parentheses to force different grouping:
if ($a == "" and ($b == "" or $c == ""))
Note that in expressions, it's conventional to use &&
and ||
rather than and
and or
-- the latter are usually used in assignments as a control structure, because they have lower precedence than assignments. E.g.
$result = mysqli_query($conn, $sql) or die (mysqli_error($conn));