Possible Duplicate:
'AND' vs '&&' as operator
Sorry for very basic question but I started learning PHP just a week ago & couldn't find an answer to this question on google/stackoverflow.
I went through below program:
$one = true;
$two = null;
$a = isset($one) && isset($two);
$b = isset($one) and isset($two);
echo $a.'<br>';
echo $b;
Its output is:
false
true
I read &&/and are same. How is the result different for both of them? Can someone tell the real reason please?
== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. === Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.
-> and => are both operators. The difference is that => is the assign operator that is used while creating an array. Thanks. It Helped.
Description. In PHP, comparison operators take simple values (numbers or strings) as arguments and evaluate to either TRUE or FALSE.
The reason is operator precedence. Among three operators you used &&
, and
& =
, precedence order is
&&
=
and
So $a
in your program calculated as expected but for $b
, statement $b = isset($one)
was calculated first, giving unexpected result. It can be fixed as follow.
$b = (isset($one) and isset($two));
Thats how the grouping of operator takes place
$one = true;
$two = null;
$a = (isset($one) && isset($two));
($b = isset($one)) and isset($two);
echo $a.'<br>';
echo $b;
Thats why its returning false for first and true for second.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With