Connecting Tech Pros Worldwide Help | Site Map

Multiple choices .. if / else

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
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?
best answer - posted by Markus
Quote:

Originally Posted by entangled View Post

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.

Quote:
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.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: 3 Weeks Ago

re: Multiple choices .. if / else


Quote:

Originally Posted by entangled View Post

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.

Quote:
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.
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: Multiple choices .. if / else


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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $arg = "T";
  3. $link_place= ($arg == 'T') ? 'Top bar' :
  4.           (($arg == 'S') ? 'Main menu bar' :
  5.           'Not in menu'));
  6. echo $link_place;
  7. ?>
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: 3 Weeks Ago

re: Multiple choices .. if / else


Quote:

Originally Posted by entangled View Post

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $arg = "T";
  3. $link_place= ($arg == 'T') ? 'Top bar' :
  4.           (($arg == 'S') ? 'Main menu bar' :
  5.           'Not in menu'));
  6. echo $link_place;
  7. ?>

Yes, but I wouldn't write it as such because it isn't too easy to read, whereas the following is easy to read:

Expand|Select|Wrap|Line Numbers
  1. switch ($arg) {
  2.     case 'T': $link_place = 'Top bar';
  3.         break;
  4.     case 'S': $link_place = 'Main menu bar';
  5.         break;
  6.     default: $link_place = 'Not in menu';
  7.         break;
  8. }
Reply