Page 1 of 1

Switch statement returning multiple values

Posted: Sat Nov 25, 2006 7:46 am
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,

Posted: Sat Nov 25, 2006 9:28 am
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.

Posted: Sat Nov 25, 2006 9:40 am
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"; }

Posted: Sat Nov 25, 2006 9:42 am
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.

Posted: Sat Nov 25, 2006 9:52 am
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,

Posted: Sat Nov 25, 2006 10:27 am
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,

Posted: Sat Nov 25, 2006 11:15 am
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;
}

Posted: Sat Nov 25, 2006 12:07 pm
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;
}