#49679 [NEW]: Suggestion: addition of inequalities example to switch docs
| From: | matt at plasmacoustics dot com | Date: | Sat, 26 Sep 2009 06:48:15 +0000 |
| Subject: | #49679 [NEW]: Suggestion: addition of inequalities example to switch docs | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-2826@lists.php.net to get a copy of this message | ||
From: matt at plasmacoustics dot com
Operating system:
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: Suggestion: addition of inequalities example to switch docs
Description:
------------
I feel the SWITCH documentation might best include code for dealing with
inequalities in a CASE, using switch(TRUE).
People are posting User Notes with implementations relying on type
conversion of a random number to boolean TRUE, as the switch argument.
This will cause a confusing logic error any time the argument is zero.
Here's an example to help people get the right idea:
Reproduce code:
---------------
<?php
$rand=rand(-3,+3);
// Good use of switch() with inequalities: note TRUE is the argument, and
all rand() outcomes are covered without overlap
switch(TRUE) {
case $rand < 0:
print "$rand is less than zero";
break;
case $rand == 0:
print "$rand is equal to zero";
break;
case $rand > 0:
print "$rand is greater than zero";
break;
}
?>
--
Edit bug report at http://bugs.php.net/?id=49679&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49679&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49679&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49679&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=49679&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49679&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=49679&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=49679&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=49679&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=49679&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=49679&r=support
Expected behavior: http://bugs.php.net/fix.php?id=49679&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=49679&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=49679&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=49679&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49679&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49679&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=49679&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=49679&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=49679&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=49679&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=49679&r=mysqlcfg