I'm from C++ background and newbie in php, somebody please help me with the difference between the two:
$totalupdatedrows = (count($rows) == 1 and !isset($rows[0]->updated_by)) ? 0 : count($rows);
$totalupdatedrows = count($rows) == 1 and !isset($rows[0]->updated_by) ? 0 : count($rows);
assume count($rows) = 1 and $rows[0]->updated_by=null. Please see that first one has only one extra wrapper parentheses.
I'm getting correct result from the first statement. I expect to get result of 0 which is the result of the first one and not the second one. I can't figure out the difference.
and
has lower precedence than the ternary operators (?
and :
). Therefore, you need to wrap it in parenthesis ((
and )
) so it is evaluated first.
Note that if you had used &&
, you would not need to use the extra parenthesis.
Checkout the PHP documentation - Operator Precedence and
is nearly last.
If you change your and
to &&
it will sit at a higher order than ?: