|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-09-11 15:41 UTC] danbrown@php.net
[2009-09-12 02:01 UTC] matt at plasmacoustics dot com
[2009-09-23 18:27 UTC] matt at plasmacoustics dot com
[2009-09-23 18:48 UTC] danbrown@php.net
[2009-10-11 03:46 UTC] matt at plasmacoustics dot com
[2009-10-12 08:46 UTC] torben@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 06 02:00:01 2026 UTC |
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 ---