2

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

?: has the lowest precedence as in C++ but in PHP "and" is even below it. I better use && to be compatible with C++.
2

Checkout the PHP documentation - Operator Precedence and is nearly last.

If you change your and to && it will sit at a higher order than ?:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.