Re: Switch statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux