Hi,

I am trying to print different messages based on what is selected. The selections are stored in a database as $perm, and the options are low, intermediate and high. I am using switch, however i only get the first case printed:

$low = "Low";
$int = "Intermediate"; 
$high = "High "; 

switch($perm) {
     case $perm == $low: echo "Low perm <br>"; break;
     case $perm == $int: echo "Intermediate perm <br>"; break;
     case $perm == $high: echo "High perm <br>"; break;
     default: echo "No information has been provided. ";
}

I have tried giving the options numerical values, however, when i retrieve the results they are still strings so comparint $perm == 1, 2, or 3 just prints the default message. Any help appreciated.

Recommended Answers

All 8 Replies

$low = "Low";
$int = "Intermediate"; 
$high = "High "; 

switch($perm) {
     case $low: echo "Low perm <br>"; break;
     case $int: echo "Intermediate perm <br>"; break;
     case $high: echo "High perm <br>"; break;
     default: echo "No information has been provided. ";
}

Thank you. I have tried it and it is still only printing the default. The database entry for $perm is Low.

Hi,

I am trying to print different messages based on what is selected. The selections are stored in a database as $perm, and the options are low, intermediate and high. I am using switch, however i only get the first case printed:

$low = "Low";
$int = "Intermediate"; 
$high = "High "; 

switch($perm) {
     case $perm == $low: echo "Low perm <br>"; break;
     case $perm == $int: echo "Intermediate perm <br>"; break;
     case $perm == $high: echo "High perm <br>"; break;
     default: echo "No information has been provided. ";
}

I have tried giving the options numerical values, however, when i retrieve the results they are still strings so comparint $perm == 1, 2, or 3 just prints the default message. Any help appreciated.

switch($perm) {
     case 'Low': 
         echo "Low perm <br>"; 
     break;
     case 'Intermediate': 
         echo "Intermediate perm <br>"; 
     break;
     case 'High': 
         echo "High perm <br>"; 
     break;
     default: 
         echo "No information has been provided. ";
}

Thanks mschroeder. That only prints the default as well :(

can you put var_dump($perm); in your code prior to the case statement and post the output here? I'm going to guess either perm is not being set or is not what you expect it to be.

You are right - this got me:

$row = mysql_fetch_array( $result );

Looped it, and it works great as it should :)

while ($row = mysql_fetch_array($result)) {
extract($row);	}

Thank you so much.

PS: How do i assign numeric values to the strings and then compare them using switch?

E.g low, med, high = 1, 2, 3 where $a, $b, $c could be either low, med, high? and both the low, med, high and $a, b,c etc are sql result queries? So something like:

'Low' = 1;
'Med' = 2;
'High' = 3;

switch ($x) {
     case $a == $b : echo "<br> Equal <br>";  break;
     case $b < $c: echo "<br> Less <br>"; break;
     case $a < $c: echo "<br> Less <br>"; break;
     default: echo "<br> No selection "; }

I've been on this so long im not even thing straight anymore :(

Thank you. I have tried it and it is still only printing the default. The database entry for $perm is Low.

I think it must be to do with $perm not being set properly, because before I posted my solution I tested it first by adding $perm = "Intermediate"; at the beginning.

(By the way, every time I see the variable $perm I think it says "sperm" :-))

Solved - thanks everyone.

edwinhermann: haha, wouldn't have noticed that if my life depended on it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.