Jump to content

switch / case


poe

Recommended Posts

is there a way to have case greater than a number like:

 

i am looping through a baseball boxscore, which i put into an array

arrBox:
Array
(
    [0] => final~2~3~4~5~6~7~8~9~10~r~h~e
    [1] => toronto~1~0~1~0~1~1~0~0~1~5~10~1
    [2] => texas~0~0~1~1~1~0~0~0~0~4~10~0
)

foreach($arrBox as $k=>$v)
{
$box[] = split("~", $v);
}
$boxhead[] = array_shift($box);
foreach($boxhead[0] as $k=>$inng)
{
switch($inng)
{
	case '>9': // greather than 9 = xtra innings
		$boxExtra = 'extra innings';
		break;
	case 'r':
		$boxScore = $box[0][$k] .'-'. $box[1][$k];
		break;
}
}

echo $boxScore .' '. $boxExtra;

 

 

is there a way to have case greater than a number like: case '>9':

it seems to be looking for literlly '>' and 9...

 

thanks

chris

Link to comment
https://forums.phpfreaks.com/topic/101396-switch-case/
Share on other sites

Yes, you can use conditionals in the case statement, but you need to switch on the value "true":

<?php
switch(true)
{
	case ($inng > 9): // greather than 9 = xtra innings
		$boxExtra = 'extra innings';
		break;
	case ($inng == 'r'):
		$boxScore = $box[0][$k] .'-'. $box[1][$k];
		break;
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/101396-switch-case/#findComment-518708
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.