Switch statement

Scot L. Harris webid at cfl.rr.com
Mon Dec 12 18:09:39 UTC 2005


On Mon, 2005-12-12 at 12:47, Dotan Cohen wrote:
> I have no problem using the switch statement when I am checking if a
> variable equals a known number, like such:
> 
> <?php
> switch ($hour) {
> case 0:
>    echo "Go to sleep.";
>    break;
> case 10:
>    echo "Good morning.";
>    break;
> case 22:
>    echo "Good night.";
>    break;
> }
> ?>
> 
> However, how do I do it if the variable is larger than a certain
> number? For instance, I want to say "Good night" if $hour is greater
> than 22?

If you want to use a switch statement you could do:

switch ($hour) {
case 0:
	echo "Go to sleep.";
	break;
case 10: 
	echo "Good Morning";
	break;
case 22:
case 23:
case 24:
	echo "Good night";
	break;
}


Just list each case statement for the numbers you want that action to
occur.  The break statement is what keeps it from falling through to the
next case.  In the example above it will print "Good night" for 22, 23,
and 24.





More information about the users mailing list