php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #49679 Suggestion: addition of inequalities example to switch docs
Submitted: 2009-09-26 06:48 UTC Modified: 2009-10-02 21:21 UTC
From: matt at plasmacoustics dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2009-09-26 06:48 UTC] matt at plasmacoustics dot com
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;
}

?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-02 20:32 UTC] fa@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

"Note: Note that switch/case does loose comparision."
 [2009-10-02 21:21 UTC] matt at plasmacoustics dot com
FA, please see http://bugs.php.net/bug.php?id=49524

Cheers
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 02:00:01 2026 UTC