#49524 [Opn->Bgs]: Misleading user note in SWITCH documentation

From: Date: Fri, 11 Sep 2009 15:41:58 +0000
Subject: #49524 [Opn->Bgs]: Misleading user note in SWITCH documentation
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-2766@lists.php.net to get a copy of this message
 ID:               49524
 Updated by:       danbrown@php.net
 Reported By:      matt at plasmacoustics dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Documentation problem
 Operating System: WinXP SP3
 PHP Version:      5.3.0
 New Comment:

Three things to point out:


1.) You posted the bug from a completely different page than the one to

which you make reference.

2.) You grossly misquoted the code of which you complained.

3.) You didn't test it yourself, because - if you had - you'd see that

it works as advertised.


Previous Comments:
------------------------------------------------------------------------

[2009-09-10 16:33:48] matt at plasmacoustics dot com

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 this bug report at http://bugs.php.net/?id=49524&edit=1



Thread (9 messages)

« previous php.doc.bugs (#2766) next »