Multiple choices .. if / else

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • entangled
    New Member
    • Nov 2009
    • 2

    Multiple choices .. if / else

    What do they call something like this ...

    <?=(($data->link_place == "T") ? "Top bar":"Main menu bar")?>

    ... it's sort of a if /else statement. But what if there are three possiblities

    T = Top bar
    M = Main menu bar
    N = No menu


    Would I have to use a normal if / ifelse / else statment?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by entangled
    But what if there are three possiblities

    T = Top bar
    M = Main menu bar
    N = No menu


    Would I have to use a normal if / ifelse / else statment?
    Yes.

    What do they call something like this ...

    <?=(($data->link_place == "T") ? "Top bar":"Main menu bar")?>

    ... it's sort of a if /else statement.
    That is known as the conditional/ternary operator.

    Mark.

    Comment

    • entangled
      New Member
      • Nov 2009
      • 2

      #3
      Thanks Mark. The link was very help too ... in my case, according to the link, with PHP, the above statement would turn into:

      Code:
      <?php
      $arg = "T";
      $link_place= ($arg == 'T') ? 'Top bar' :
                (($arg == 'S') ? 'Main menu bar' :
                'Not in menu'));
      echo $link_place;
      ?>
      Last edited by Atli; Nov 1 '09, 05:36 PM. Reason: Please use [code] tags when posting code.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by entangled
        Thanks Mark. The link was very help too ... in my case, according to the link, with PHP, the above statement would turn into:

        Code:
        <?php
        $arg = "T";
        $link_place= ($arg == 'T') ? 'Top bar' :
                  (($arg == 'S') ? 'Main menu bar' :
                  'Not in menu'));
        echo $link_place;
        ?>
        Yes, but I wouldn't write it as such because it isn't too easy to read, whereas the following is easy to read:

        Code:
        switch ($arg) {
            case 'T': $link_place = 'Top bar';
                break;
            case 'S': $link_place = 'Main menu bar';
                break;
            default: $link_place = 'Not in menu';
                break;
        }

        Comment

        Working...