I have this ternary operator which i am trying to understand by converting it to normal ifelse
$countDinner[$invitation->getDinner()->getId()] = isset($countDinner[$invitation->getDinner()->getId()]) ? $countDinner[$invitation->getDinner()->getId()] + 1 : 1;
but I am not getting the correct result
$countDinner[$invitation->getDinner()->getId()] = "";
if (isset($countDinner[$invitation->getDinner()->getId()])) {
$countDinner[$invitation->getDinner()->getId()] = $countDinner[$invitation->getDinner()->getId()] + 1;
}else{
$countDinner[$invitation->getDinner()->getId()] = 1;
}
What am i doing wrong here?
In doing
$countDinner[$invitation->getDinner()->getId()] = "";
you're creating a condition that means
if (isset($countDinner[$invitation->getDinner()->getId()])) {
will always be true.... you're ensuring that it is created before testing whether it exists, so (of course) it will always exist.
Get rid of the
$countDinner[$invitation->getDinner()->getId()] = "";
line
You can also simplify
$countDinner[$invitation->getDinner()->getId()] = $countDinner[$invitation->getDinner()->getId()] + 1;
to
$countDinner[$invitation->getDinner()->getId()]++;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments