Jump to content

Switch()


Cod-nes

Recommended Posts

I'm having problems with switch(). The script works fine when I only put one value. But when I try to do two values it executes something weird. When the $ns=s2, it executes case "s1" or another case.

$ns=$GET["ns"];switch ($cookie || $ns){case "s1":echo $sr1;break;case "s2":echo "hi";break;case "s3":echo $sr3;break;case "s4":echo $sr4;break;case "s5":echo $sr5;break;case "s6":echo $sr6;break;case "s7":echo $sr7;break;default:echo "hi";}

Link to comment
Share on other sites

How does switching for the value ($cookie or $ns) work? This will return true or false, depending on the values, and if true will execute the "s1" case, and if false will use default.I don't understand what you are trying to achieve with the or operator...

Link to comment
Share on other sites

|| returns a boolean value. Non-boolean values are cast as boolean and called "truthy" or "falsy." Strings are only falsy when empty.

switch (empty($cookie) ? $ns : $cookie)

This uses a boolean correctly and achieves the result which I consider the most humanly reasonable for the given code. See the ternary operator.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...