Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.

Reply With Quote
  #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
Reply With Quote
  #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.

Reply With Quote
  #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/
Reply With Quote
  #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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #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/
Reply With Quote
  #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/
Reply With Quote
  #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/
Reply With Quote
  #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
:::::::::::::::::::::::::::::::::
Reply With Quote
  #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
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:49 AM.


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