...

View Full Version : Switch Statement



epheterson
11-11-2007, 04:51 AM
I'm not understand why this doesn't work:


echo '"'. $default_class . '"';
// Set ticket price based on chosen class
switch($default_class) {
case "coach":
$ticketprice = 99.99;
case "business":
$ticketprice = 149.99;
case "first":
$ticketprice = 199.99;
default:
$ticketprice = 89.99;
}
echo '"'. $ticketprice . '"';

It echos the right $default_class, but doesn't set the right ticket price. It always defaults to 89.99.

Any ideas?

Inigoesdr
11-11-2007, 05:21 AM
You forgot to add breaks (http://php.net/break) after the cases.

helraizer
11-11-2007, 11:43 PM
I'm not understand why this doesn't work:


echo '"'. $default_class . '"';
// Set ticket price based on chosen class
switch($default_class) {
case "coach":
$ticketprice = 99.99;
case "business":
$ticketprice = 149.99;
case "first":
$ticketprice = 199.99;
default:
$ticketprice = 89.99;
}
echo '"'. $ticketprice . '"';

It echos the right $default_class, but doesn't set the right ticket price. It always defaults to 89.99.

Any ideas?

As Inigoesdr said you need to add the breaks in. So it'd look like:


echo '"'. $default_class . '"';
// Set ticket price based on chosen class
switch($default_class) {
case "coach":
$ticketprice = 99.99;
break;
case "business":
$ticketprice = 149.99;
break;
case "first":
$ticketprice = 199.99;
break;
default:
$ticketprice = 89.99;
break;
}
echo '"'. $ticketprice . '"';

just incase you haven't done it already.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum