convert ternary operator to if else

Saadia

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?

Mark Baker

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

If without else ternary operator

From Dev

If else with Ternary operator

From Dev

Ternary Operator if else if issue

From Dev

How can I convert this if else statement into ternary operator?

From Dev

String Convert To Ternary Operator

From Dev

How to Convert this to a ternary operator

From Dev

String Convert To Ternary Operator

From Dev

Convert if statement to ternary operator

From Dev

Simplify if else condition with ternary operator

From Dev

Python ternary operator and assignment in else

From Dev

advantage of ternary operator over if else?

From Dev

Javascript Ternary operator with empty else

From Dev

Nullpointer exception with conditional operator, (ternary operator) but not with if else

From Java

Ternary operator is twice as slow as an if-else block?

From Dev

{{ !$last && ', ' || '.'}} is this working as ternary operator or if-else condition

From Dev

{{ !$last && ', ' || '.'}} is this working as ternary operator or if-else condition

From Dev

"Do nothing" in the else-part of the ternary operator?

From Dev

Convert if else into ternary in ruby slim(temple engine)

From Dev

Trying to convert if else statement into ternary in javascript

From Dev

how can I convert this ternary into a if/else?

From Dev

convert some lines of php statement into ternary operator

From Dev

convert some lines of php statement into ternary operator

From Java

Java ternary operator vs if/else in <JDK8 compatibility

From Dev

nested ternary operator vs nested if else, which is better in readability purpose

From Dev

Why can I use If Else syntax and not a ternary operator?

From Dev

How to write if else condition using ternary operator in jstl?

From Dev

Comparisions using ternary operator vs if else vs switch case (Performance)

From Dev

Java Ternary operator outputs different result than if else statement

From Dev

How would I write this if else statement as ternary operator

Related Related

  1. 1

    If without else ternary operator

  2. 2

    If else with Ternary operator

  3. 3

    Ternary Operator if else if issue

  4. 4

    How can I convert this if else statement into ternary operator?

  5. 5

    String Convert To Ternary Operator

  6. 6

    How to Convert this to a ternary operator

  7. 7

    String Convert To Ternary Operator

  8. 8

    Convert if statement to ternary operator

  9. 9

    Simplify if else condition with ternary operator

  10. 10

    Python ternary operator and assignment in else

  11. 11

    advantage of ternary operator over if else?

  12. 12

    Javascript Ternary operator with empty else

  13. 13

    Nullpointer exception with conditional operator, (ternary operator) but not with if else

  14. 14

    Ternary operator is twice as slow as an if-else block?

  15. 15

    {{ !$last && ', ' || '.'}} is this working as ternary operator or if-else condition

  16. 16

    {{ !$last && ', ' || '.'}} is this working as ternary operator or if-else condition

  17. 17

    "Do nothing" in the else-part of the ternary operator?

  18. 18

    Convert if else into ternary in ruby slim(temple engine)

  19. 19

    Trying to convert if else statement into ternary in javascript

  20. 20

    how can I convert this ternary into a if/else?

  21. 21

    convert some lines of php statement into ternary operator

  22. 22

    convert some lines of php statement into ternary operator

  23. 23

    Java ternary operator vs if/else in <JDK8 compatibility

  24. 24

    nested ternary operator vs nested if else, which is better in readability purpose

  25. 25

    Why can I use If Else syntax and not a ternary operator?

  26. 26

    How to write if else condition using ternary operator in jstl?

  27. 27

    Comparisions using ternary operator vs if else vs switch case (Performance)

  28. 28

    Java Ternary operator outputs different result than if else statement

  29. 29

    How would I write this if else statement as ternary operator

HotTag

Archive