Re: #49524 [NEW]: Misleading user note in SWITCH documentation
2009/9/10 matt at plasmacoustics dot com <doc-bugs@lists.php.net>:
> From: matt at plasmacoustics dot com
> Operating system: WinXP SP3
> PHP version: 5.3.0
> PHP Bug Type: Documentation problem
> Bug description: Misleading user note in SWITCH documentation
>
> Description:
> ------------
> I feel the documentation on SWITCH might best include the appropriate code
> for dealing with inequalities in a CASE, using switch(TRUE), because people
> are posting User Notes with incorrect implementations.
>
> Eg from: sedativchunk at gmail dot com
>
> <?php
>
> $rand=rand(1,50);
>
> switch($randomizer) {
> case ($randomizer <= 20):
> $font_size = "11";
> break;
> }
>
> ?>
>
> This isn't how switch() works! This will compare the value of
> ($randomizer) with the value of the inequality ($randomizer <= 20), right?
>
> Here is a better example of why it strikes me as dangerous:
>
> <?php
>
> $zero=0;
>
> switch($zero) {
> case($zero > 1):
> print '0 > 1? No!';
> break;
> }
>
> ?>
>
> The above code will go into the case because it sees:
> $zero[FALSE] == ($zero > 1)[also FALSE], leading to great coder
> confusion.
>
> The ACTUAL desired behaviour should be implemented with the code:
>
> <?php
>
> $zero=0;
>
> switch(TRUE) {
> case($zero > 1):
> print '0 > 1? No!';
> break;
> }
>
> ?>
>
> Could we add an inequality example to the documentation and mend the
> user-contributed note #88643?
>
> Reproduce code:
> ---------------
> ---
> From manual page: language.operators.comparison
> ---
>
>
>
> --
> Edit bug report at http://bugs.php.net/?id=49524&edit=1
> --
> Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49524&r=trysnapshot52
> Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49524&r=trysnapshot53
> Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49524&r=trysnapshot60
> Fixed in SVN: http://bugs.php.net/fix.php?id=49524&r=fixed
> Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49524&r=needdocs
> Fixed in release: http://bugs.php.net/fix.php?id=49524&r=alreadyfixed
> Need backtrace: http://bugs.php.net/fix.php?id=49524&r=needtrace
> Need Reproduce Script: http://bugs.php.net/fix.php?id=49524&r=needscript
> Try newer version: http://bugs.php.net/fix.php?id=49524&r=oldversion
> Not developer issue: http://bugs.php.net/fix.php?id=49524&r=support
> Expected behavior: http://bugs.php.net/fix.php?id=49524&r=notwrong
> Not enough info: http://bugs.php.net/fix.php?id=49524&r=notenoughinfo
> Submitted twice: http://bugs.php.net/fix.php?id=49524&r=submittedtwice
> register_globals: http://bugs.php.net/fix.php?id=49524&r=globals
> PHP 4 support discontinued: http://bugs..php.net/fix.php?id=49524&r=php4
> Daylight Savings: http://bugs.php.net/fix.php?id=49524&r=dst
> IIS Stability: http://bugs.php.net/fix.php?id=49524&r=isapi
> Install GNU Sed: http://bugs.php.net/fix.php?id=49524&r=gnused
> Floating point limitations: http://bugs..php.net/fix.php?id=49524&r=float
> No Zend Extensions: http://bugs.php.net/fix.php?id=49524&r=nozend
> MySQL Configuration Error: http://bugs..php.net/fix.php?id=49524&r=mysqlcfg
>
>
> --
> PHP Documentation Bugs Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Confused...
<?php
$rand=rand(1,10);
echo "Generated $rand : ";
switch($rand){
case ($rand <= 3):
echo '$rand is between 0 and 3';
break;
case (3 < $rand && $rand <= 6):
echo '$rand is between 4 and 6';
break;
case (6 < $rand && $rand <= 9):
echo '$rand is between 7 and 9';
break;
default :
echo '$rand is 10';
}
outputs correct values.
Generated 8 : $rand is between 7 and 9
Generated 5 : $rand is between 4 and 6
Generated 5 : $rand is between 4 and 6
I think there is a typo in that $rand is the variable generated, but
$randomize is used in the code.
switch() simply defines the value to be evaluated.
case() does the check
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
Thread (9 messages)