phpshortcode

Nested shortcode conditions evaluate unexpectedly


I'm using this below example code:

<?php
$id = "a";
echo $id == "a" ? "Apple" : $id == "b" ? "Bat" : $id == "c" ? "Cat" : $id == "d" ? "Dog" : "Others";

I want to get output as Apple, but I got Dog.


Solution

  • <?php
    
    $id = "a";
    
    echo $id == "a" ? "Apple" : ($id == "b" ? "Bat" : ($id == "c" ? "Cat" : ($id == "d" ? "Dog" : "Others")));