Adding more options to an If Statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kailea
Forum Newbie
Posts: 1
Joined: Thu Dec 04, 2003 12:16 am

Adding more options to an If Statement

Post by Kailea »

I'm rather new at PHP and having been trying to modify these two lines to work with four options instead of two

if ($this->db->VARS["allow_age"]==1) {
$AGE= ($this->age==20) ? "&nbsp;<img src=\"img/20.gif\" width=\"12\" height=\"12\">" : "&nbsp;<img src=\"img/30.gif\" width=\"12\" height=\"12\">";
}


if ($this->db->VARS["allow_age"]==1) {
$AGE = ($row['age']==20) ? "&nbsp;<img src=\"img/20.gif\" width=\"12\" height=\"12\">" : "&nbsp;<img src=\"img/30.gif\" width=\"12\" height=\"12\">";
}

I want this to display a different image if the variable is 20, 30, 40 or 50. and not just 20 and 30. I have tried everything I can think of. Any help. Thanks ahead of time
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

How about using the switch statement, rather than if.

read up here for examples:
http://ca2.php.net/manual/en/control-st ... switch.php
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post by leebo »

You may be better off using CASE than having 4 different ifs

$AGE= ($this->age==20);

Switch ( $AGE ) {

case "20":

do whatever

break;

case "30":

do whatever

break;

case "40":

do whatever

break;

case "50":

do whatever

break;

default:

echo "sorry no age selected";
}
Post Reply