I just copy and pasted your switch statement and it worked fine for me
the way you described it needed to work.
One thing I did notice, there's a semi-colon on the >= 2400 CASE, but it
still seemed to work ok for me (maybe I noticed and fixed that before I
tested >= 2400).
What error are you getting or what results using what values for $qty?
One thing I'll point out that I didn't know before but someone on here
enlightened me to was that there's two ways to use switch, one where you
use switch($variable) and your case statements are what the $variable is
equal to, or you can use other kinds of conditions in your case
statement, then it doesn't really matter what your switch() contains:
Switch ($variable) {
case 0:
echo "zero";
break;
case 1:
echo "one";
break;
default:
echo "other;
break;
}
Or...
Switch (TRUE) {
case ($variable < 0):
echo "Negative";
break;
case ($variable < 100):
echo "Under 100";
break;
case ($variable < 1000):
echo "Under 1000";
break;
default:
echo ">= 1000";
break;
}
You can leave out break statements if you want it to execute certain
things for multiple cases in an additive fashion:
$sendemail = TRUE;
$tofield = "";
$ccfield = "";
$bccfield = "";
Switch ($sendemail) {
case ($bcc <> ""):
$bccfield = LookupEmailAddresses($bcc);
case ($cc <> ""):
$ccfield = LookupEmailAddresses($cc);
case ($to <> ""):
$tofield = LookupEmailAddresses($cc);
SendEmail($tofield,$ccfield,$bccfield);
break;
default:
echo "No recipients, email not sent";
break;
}
In this case, it only processes BCC, CC and TO fields if they contain
data. You might just enter "Trevor" and it runs the function
LookupEmailAddresses to find "[EMAIL PROTECTED]" and inserts
that into the $bccfield variable.
Lastly, you can stack case clauses or use an OR operator to do something
when either condition is true:
Switch (TRUE) {
case ($variable < 0):
case ($variable >= 1000):
echo "Out of range (either too large or too small)";
break;
case ($variable < 100):
echo "Between 0 and 100";
break;
case ($variable < 1000):
echo "Between 100 and 1000";
break;
default:
echo "Some imaginary value";
break;
}
You could also express that as:
case ($variable < 0 OR $variable >= 1000):
I'm sure there are other ways to use Switch/Case. I know you didn't ask
for a whole tutorial and stuff, but it's a good thing to know how to
use, very useful (and a lot nicer to play with than a bunch of nested
if/then/else's).
Did I screw anything up in those examples? I'm sure someone will point
it out. Hah.. Any other creative uses for Switch that I missed?
-TG
> -----Original Message-----
> From: Tony Devlin [mailto:[EMAIL PROTECTED]
> Sent: Thursday, November 11, 2004 12:35 PM
> To: Php-Windows
> Subject: [PHP-WIN] Question concerning SWITCH and Comparisons
>
>
> If my understanding of SWITCH is correct, it stops at the
> first case that
> matches the result, correct?
>
> So then.. If I have a switch as such;
>
> switch($qty){
>
> case ($qty < 1600): //$qty is less than 1600
> do something
> break;
>
> case ($qty < 2400): //$qty is less than 2400
> do something
> break;
>
> case ($qty >= 2400); //$qty is greater than or equal to 2400
> do something
> break;
>
> default:
> do something
> break;
> }
>
> Then assuming $qty = 800, it should stop at the first case? right?
>
> a $qty of 1601 should stop at the second case?
>
> and a qty of greater than or equal to 2400 should stop at the
> last case?
>
> Am I correct with my switch statement then?
>
> My desired result is;
>
> If quantity is less than 1600 set price = whatever;
> or if quantity is equal to 1600 and less than 2400 set price
> = whatever,
> or finally if quantity is equal to or greater than 2400 set price =
> whatever.
>
> I'm having difficulties for whatever reason to come up with a proper
> comparison
> for the switch to work as described. Can someone help me
> iron out this
> small problem?
>
>
> Much thanks for the help,
>
> Tony D.
>
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php