recent posts

Taking It to the Street

One Foot on a Stone

I just swallowed something in my coffee.

Craziest Thing You Did at Disneyland

SBC Call Annoyance Bureau, Heal Thyself

The Perfect Hot Buttered Rum

Shit, 2005!

Two Headlines from Todays New York Times

The New Office Craze

A Very Important Call from Ken Hughes

archives

Wednesday, January 12, 2005
In reply to Alex Fung :
The following code doesn't work :

$x = 18;
$y = 6;

switch ($x) {
case (($y * 4) || (9 * 3)):
echo "Member";
break;
default:
echo "Not a member";
}
?>

Why :
want to test if $x == $y*4 or $x == 9*3 ($x == (($y*4)||(9*3))
However the case statement evaluate the value of (($y*4)||(9*3)) that is always true because 9*3=27 (!=0)
That's why this code always return true when $x != 0.
The correct code would be :

$x = 18;
$y = 6;

switch ($x) {
case (($y * 4)):
case ((9*3)):
echo "Member";
break;
default:
echo "Not a member";
}
?>

Boolean logic work inside case statement, you just need to know that the expression in the case statement is first evaluated then compared with the evaluated value in the switch statement.