Switch statement returning multiple values

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Switch statement returning multiple values

Post by impulse() »

I have a switch statement which checks conditions for values in a MySQL DB and should returned 'Unavailable' if a value is 0 and 'Available' if a value is 1. But at the moment the first check runs fine but the rest return both results. Can somebody help me please.
My code is:


Code: Select all

switch ($res) {
          case ($res['lowloader'] == 0):
            echo "Lowloader: <b>Unavailable</b><br>";
          case ($res['lowloader'] == 1):
            echo "Lowloader: <b>Available</b><br>";
          case ($res['articflatbed'] == 0):
            echo "Artic flatbed: <b>Unavailable</b><br>";
          case ($res['articflatbed'] == 1):
            echo "Artic flatbed: <b>Available</b><br>";
          case ($res['car'] == 0):
            echo "Car: <b> Unavailable</b><br>";
          case ($res['car'] == 1):
            echo "Car: <b> Available </b><br>";
          case ($res['vantauliner'] == 0):
            echo "Vantauliner: <b> Unavailable</b><br>";
          case ($res['vantauliner'] == 1):
            echo "Vantauliner: <b> Available</b><br>";
          case ($res['vanlwb'] == 0):
            echo "Vanlwb: <b> Unavailable </b><br>";
          case ($res['vanlwb'] == 1):
            echo "Vanlwb: <b> Available </b><br>";
          case ($res['vanswb'] == 0):
            echo "Vanswb: <b> Unavailable </b><br>";
          case ($res['vanswb'] == 1):
            echo "Vanswb: <b> Available </b><br>";
          case ($res['tautliner75t'] == 0):
            echo "Tautliner75: <b> Unavailable </b><br>";
          case ($res['tautliner75t'] == 1):
            echo "Tautliner75: <b> Available </b><br>";
          case ($res['box75t'] == 0):
            echo "Box75t: <b> Unavailable </b><br>";
          case ($res['box75t'] == 1):
            echo "Box75t: <b> Available </b><br>";
          case ($res['vanmwb'] == 0):
            echo "Vanmwb: <b> Unavailable </b><br>";
          case ($res['vanmwb'] == 1):
            echo "Vanmwb: <b> Available </b><br>";

        }
'Lowloader' only show Available, as expected. But the rest show both 'Available' and 'Unavailable'.

Regards,
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
switch(1) {
	case 1:
		echo '1';
	case 2:
		echo '2';
}
?>
prints 12. The next case statement is not an implicit break (like e.g. in c#).

Code: Select all

<?php
switch(1) {
	case 1:
		echo '1';
		break;
	case 2:
		echo '2';
		break;
}
?>
see http://de2.php.net/break.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I want it to run through each condition and check though. If I break out after the first condition statement is true then it doesn't run through the rest of the switch loop.

My expected output was something along the lines of:

lowloader: Available
articflatbed : Available
car : Unavailable
vantautliner: Available


If I used an IF statement for every single condition it would work. Eg

Code: Select all

if ($res['lowloader'] == 0) { echo "Not available"; }
if ($res['lowloader'] == 1) { echo "Available"; }
if ($res['articflatbed'] == 0) { echo "Not available"; }
if ($res['articflatbed'] == 1) { echo "Available"; }
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

impulse() wrote:I want it to run through each condition and check though.
Switch doesn't work that way.
impulse() wrote:If I used an IF statement for every single condition it would work.
Then use it.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I was certain I'd learned from tutorials that switch statements were used in place of IF statements when the amount of conditions got quite large. I will revise the use of switch statements now though.

Stephen,
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

impulse() wrote:I was certain I'd learned from tutorials that switch statements were used in place of IF ELSE statements when the amount of conditions got quite large. I will revise the use of switch statements now though.

Stephen,
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

jayshields wrote:
impulse() wrote:I was certain I'd learned from tutorials that switch statements were used in place of IF ELSE statements when the amount of conditions got quite large. I will revise the use of switch statements now though.

Stephen,
The trick is to switch on true, not the variable you're testing...

Code: Select all

switch(TRUE) {
   case ($res == "this"): echo "it said this";break;
   case ($res == "dance like a chicken") echo "cluck cluck step cluck...";break;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You're comparing $res both times and you're using break. impulse() does not.

Does your example provide any advantage over a "regular" switch/case statement?

Code: Select all

switch($res) {
	case "this":
		echo "it said this";
		break;
	case "dance like a chicken":
		echo "cluck cluck step cluck...";
		break;
}
Post Reply