Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 14th, 2003, 09:13 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

  #2 (permalink)  
Old August 15th, 2003, 04:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

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

:D:D:D:D

the genuine genius
  #3 (permalink)  
Old August 15th, 2003, 06:09 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #4 (permalink)  
Old August 18th, 2003, 01:43 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
  #5 (permalink)  
Old August 18th, 2003, 09:59 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::
  #6 (permalink)  
Old August 18th, 2003, 10:59 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
  #7 (permalink)  
Old August 18th, 2003, 11:15 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
  #8 (permalink)  
Old August 18th, 2003, 11:16 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Take care,

Nik
http://www.bigaction.org/
  #9 (permalink)  
Old August 19th, 2003, 08:41 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::
  #10 (permalink)  
Old August 19th, 2003, 01:39 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
:::::::::::::::::::::::::::::::::




Similar Threads
Thread Thread Starter Forum Replies Last Post
Case statement cole SQL Language 3 May 8th, 2005 03:02 PM
case statement Hudson40 Access VBA 1 February 11th, 2005 11:31 AM
case statement troubles!! ronny Classic ASP Databases 2 April 10th, 2004 07:32 PM
Using A CASE Statement fastcorvette Access 5 December 24th, 2003 01:39 PM
HELP - IF ELSE & CASE statement savoym SQL Language 3 September 12th, 2003 06:35 AM





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