php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #49524 Inequalities example in SWITCH documentation
Submitted: 2009-09-10 16:33 UTC Modified: 2009-10-12 08:46 UTC
From: matt at plasmacoustics dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3.0 OS:
Private report: No CVE-ID: None
 [2009-09-10 16:33 UTC] 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
---



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-11 15:41 UTC] danbrown@php.net
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.
 [2009-09-12 02:01 UTC] matt at plasmacoustics dot com
Thanks for pointing out the errors. You're right, there are many mistakes in the submission, do feel free to delete it.

I do maintain though that the SWITCH argument would be best as TRUE to guarantee the expected behaviour instead of relying on the type conversion.

jonybd at yahoo dot com has a note that deals with inequalities. An official example would be well received if you so wished. Cheers.
 [2009-09-23 18:27 UTC] matt at plasmacoustics dot com
Dan:

There is another example, see http://au.php.net/manual/en/control-structures.switch.php#93342

If $totaltime is ZERO, a confusing semantic error would result. Reliance on the arbitrary argument being interpreted as TRUE seems widespread.

Why not add a SWITCH example covering INEQUALITIES to the documentation?
 [2009-09-23 18:48 UTC] danbrown@php.net
I think that's a good idea, Matt.  Would you please re-submit that as a 
suggestion?  Submitting it as a "Documentation problem," but with the 
subject something along the lines of "Suggestion: Addition of Example to 
Switch Page" should suffice.

Also, feel free to supply an example you think would be of good use for 
inclusion as an official example.  Just be sure to test and re-test it, 
just as we would ourselves.  Some folks out there in userland make a 
habit of simply copying and pasting code, thinking it will always work 
as advertised (and be safe to run in the first place)!  ;-P

Thanks for your efforts, Matt.  We absolutely appreciate when folks take 
the time to help the community, such as you're continuing to do 
yourself!
 [2009-10-11 03:46 UTC] matt at plasmacoustics dot com
Dan,

Here was the submission a fortnight ago:
http://bugs.php.net/bug.php?id=49679
as per your suggestion.

It was flagged 'Bogus'.

This week I debugged two clients scripts both suffering from this exact logic error. Both developers had built on bad examples from SWITCH documentation comments.

I brought attention to the widespread misunderstanding, resulting in your snappy response.

I suggested a code example that will make this clear in future, which was ignored.

If both you and fa don't wish to document the correct behaviour, please just say so. Let us submitters and you developers try to read more carefully and demonstrate some cohesion as a team.
 [2009-10-12 08:46 UTC] torben@php.net
If this sort of thing is documented at all, I would love to see any 
mention of switch() being used this way be prefaced with wording along 
the lines of "Even though it's bad form, you can use switch() like 
this. . .". This stuff can be more readably and more efficiently be 
implemented using if..elseif blocks. if..elsif is also less error-prone 
(as this bug report proves).

As for coders using what they find in the User Notes, that's strictly 
at their own risk. While we try to keep on top of the notes, doing so 
is a gargantuan task and keeping edge cases, syntax tricks, and bad 
code out just doesn't always happen.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 02:00:01 2026 UTC