p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   case statement (http://p2p.wrox.com/showthread.php?t=2826)

jakeone August 14th, 2003 09:13 PM

case statement
 
Hello,

I tried the following:

switch($new_t1)
case "D","W":
if ($new_p1<1 OR $new_p1>30) {
$t1_errMsg = "The number of billing cycle days/weeks must be between 1 and 30";
}
break;

but received an error stating, "unexpected T_CASE, expecting ':' or '{' "

I assume case "D","W": is what's causing the problem. Do I have to treat these as separate case statements? If so, seems rather inefficient.

Thanks.


Moharo August 15th, 2003 04:48 AM

switch statement is used instead of many if...else.
problem causes your case "D","W": you gotta have those separate...

[8D]:D:D:D:D

the genuine genius

p15ed August 15th, 2003 06:09 AM

Try

switch($new_t1)
{
case "D":
case "W":
if ($new_p1<1 OR $new_p1>30) {
$t1_errMsg = "The number of billing cycle days/weeks must be between 1 and 30";
break;
}

The trick with switch statements is that the Php processor will 'tumble' through each case until it finds a 'break' keyword. thus many cases can execute the same code if placed one after another.


nikolai August 18th, 2003 01:43 PM

Be careful of your case statments though!

The most common mistake is a forgotten "break" statement, which causes your code to "fall through" to cases you didn't intent to execute!


Take care,

Nik
http://www.bigaction.org/

richard.york August 18th, 2003 09:59 PM

Quote:

quote:switch statement is used instead of many if...else.
problem causes your case "D","W": you gotta have those separate...
Actually,
this is not entirely true. I wrote a few test scripts using the switch control structure, and found that the 'OR' or '||' operator worked fine within a switch control structure... as well as with the other operators... so in your case you could use:

Code:

switch($new_t1) {

    case "D" || "W":

        if ($new_p1<1 OR $new_p1>30) {

            $t1_errMsg = "The number of billing cycle days/weeks must be between 1 and 30";
        }
    break;
}

I found no discussion of this in the PHP manual under control structures, however this worked fine in my test script.

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

nikolai August 18th, 2003 10:59 PM

Rich, that sounds pretty dangerous, actually. I would stay away from it for two main reasons:

1) It's not a feature supported by most other languages implementing "switch",

2) It might "work" for the wrong reasons, meaning that it might accept invalid input into that case statement.

This reminds me of why PHP implemented equivalence operators (=== and !==). I don't have the time to go into it now, maybe I'll play with some tests and get back to you tomorrow.



Take care,

Nik
http://www.bigaction.org/

nikolai August 18th, 2003 11:15 PM

Okay, I realized it would only take a minute to test, and I'm glad I did, because my hunch (#2 above) was correct.

In a switch statement, you're essentially taking the value in the switch parens and comparing whether it's == the expression in the case statement.

Therefore,

switch($foo) {
  case "A" || "B":
  {
     BLOCK_A;
  }
}

really means (at it's strictest)

if(($foo) == ("A" || "B"))
{
   BLOCK_A;
}

Or it might mean this (subtle difference, but dangerous!)
if($foo == "A" || "B")
{
   BLOCK_A;
}

In the first case, we are comparing a variable with "true". Since any non-boolean will be converted to a boolean for the comparison, only non-zero integers and non-empty strings will actually return false.

In the second case, we are comparing a variable with "A" and then ORing with "B". Since "B" is a string in a boolean context, it's converted to the boolean true. If either side of a boolean expression is true, then the entire expression is true, which means that case statement will always execute.

I didn't write a detailed enough test to figure out WHICH of the two possibilities is what's really happening, but this test demonstrates that it is, in fact, incorrect behavior none the less:

Code:

<?php

$foo = "D";

switch($foo)
{
    case "A" || "B":
    {
        echo 'case "A" || "B":' . "\n";
        break;
    }
    case "D":
    {
        echo 'case "D"' . "\n";
        break;
    }
}

?>

Take care,

Nik
http://www.bigaction.org/

nikolai August 18th, 2003 11:16 PM

Forgot to mention -- the test script I posted outputs 'case "A" || "B":'.


Take care,

Nik
http://www.bigaction.org/

richard.york August 19th, 2003 08:41 AM

Thanks for the detailed response Nik.
I was a little in the dark on just how that worked! Even more so since there was no mention of it in the PHP manual.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

richard.york August 19th, 2003 01:39 PM

So in writing the if statement equivelent would

if(($foo) == ("A" || "B"))
{
   BLOCK_A;
}

..be the better way?
Or would a more explicit approach be best suited...

if ($foo == "A" || $foo == "B") {

     BLOCK_A;

}

Is what you have written there the exact equivalent?
Actually I might as well just write my own test here...

Code:

<?php

$foo = "D";

if(($foo) == ("A" || "B")) {

    echo "To be!<br />";

} else {

    echo "Or not to be!";

}

if ($foo == "A" || $foo == "B") {

    echo "To be!<br />";

} else {

    echo "Or not to be!<br />";

}

?>

So apparently the first statement is not equivalent with the second as what you mentioned before with the conversion to boolean values is happening!
And the output was:

To be!
Or not to be!

... is there a shortcut???

In terms of the '===' operator, this is best suited for case sensitive or type data comparison, right? I think what I am getting from the manual page that this is for the explicit type comparision of data... well I'll answer my own question again with this little test...

Code:

<?php

$a = 1;
$b = "1";

if ($a === $b) {

    echo "it's true<br />";

} else {

    echo "Not so!<br />";

}

$a = "a";
$b = "A";

if ($a === $b) {

    echo "it's true<br />";

} else {

    echo "Not so!<br />";

}

?>

Since each example evaluates to false I think I'm on the right track here.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::


All times are GMT -4. The time now is 10:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.