Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webrobert's avatar

elegant solution for counterpart?

$type equals "Like" or "Dislike" I want the counterpart.

  // This works just fine but I feel like there's a sexier way...
  $counterpart = ($type == "Like") ? 'Dislike' : 'Like';
0 likes
3 replies
Tray2's avatar

That is good enough. You might consider using numeric values that references the like or dislike though. It will take up less pace in the database.

MichalOravec's avatar
Level 75

This will work as well

$counterpart = ['Like', 'Dislike'][$type === 'Like'];

and in your example parentheses are not required

$counterpart = $type == 'Like' ? 'Dislike' : 'Like';

Please or to participate in this conversation.