p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Switch Problem-Question


Message #1 by "Periklis Paschidis" <perikpas@o...> on Thu, 26 Sep 2002 18:26:38
Hello to all.

I have the folowing code:

switch ($Age)
{
case $Age <= 12:
echo You ar to smoll to go inside!";
break;
case $Age > 12 && $Age < 16:
echo You may enter only the teens section!";
break;
default:
echo "If you see this message then something is going wrong!";
}

Now, if i put a value from 12 and down then its works fine, the right 
message is seen on screen. But!
If tha ege is 13 or hier then the default message is seen on screen and 
not the correct. WHAY?!

Dues the "case:" command not support checking two or more values?

Plz help (snif!) ;-(
Message #2 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Thu, 26 Sep 2002 11:02:35 -0700
> switch ($Age)
> {
> case $Age <=3D 12:
> echo You ar to smoll to go inside!";
> break;
> case $Age > 12 && $Age < 16:
> echo You may enter only the teens section!";
> break;
> default:
> echo "If you see this message then something is going wrong!";
> }

You're using switch incorrectly.

A switch goes through the block of code, matching the value of (in your 
case) $Age against the value in the case statement. It does not make 
comparisons (e.g., <=3D && ).

Given your business logic, you've got two choices:

switch($Age)
{
	case 0:
	case 1:
	... // not real code!
	case 11:
		echo "you are too young!";
		break;
	case 12:
	case 13:
	case 14:
	case 15:
		echo "You are a teen";
		break;
	default:
		echo: "You are too old";
}


if ( $Age <=3D 12 )
{
	echo "You are too young!";
}
elseif ( $Age >=3D 16 )
{
	echo "You are too old!";
}
else
{
	echo "You are a teen";
}


dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************

Message #3 by "Periklis Paschidis" <perikpas@o...> on Thu, 26 Sep 2002 21:53:57
Thank you for replying to me. Yes! Your right! I just wasnt sure is it was 
my fault or if my php had a problem. Now i undestand how "Switch" works. 
So... Conclusion: case duesn't make <= e.t.c. commands.

Thans again :)
Message #4 by "Nikolai Devereaux" <yomama@u...> on Mon, 30 Sep 2002 11:49:03 -0700
Here's a trick you can use with switches to make them emulate if/else chains.

Your switch version:  (doesn't work)

switch ($Age)
{
  case $Age <= 12:
    echo You ar to smoll to go inside!";
    break;
  case $Age > 12 && $Age < 16:
    echo You may enter only the teens section!";
    break;
  default:
    echo "If you see this message then something is going wrong!";
}


dsb's if/else version:

if ( $Age <= 12 )
{
  echo "You are too young!";
}
elseif ( $Age >= 16 )
{
  echo "You are too old!";
}
else
{
  echo "You are a teen";
}


Nik's switch version:

switch(true)
{
  case ($Age < 13):
    echo "You are to young to go inside!";
    break;
  case ($Age > 16):
    echo "You are too old to go inside!";
    break;
  default:
    echo "may enter only the teens section!";
}



See what's going on?  I used 'true' as the basis for comparison in the switch
statement.  The switch statement then compares 'true' to the expressions ($Age
< 13) and ($Age > 16).  If the user is not younger than 13 and not older than
16, then they MUST be between those ages, which is why the default case is the
"teens section!" one.


Hope this helps!

nik





  Return to Index