Bug #77584 [Com]: switch case problem
Edit report at https://bugs.php.net/bug.php?id=77584&edit=1
ID: 77584
Comment by: sjon at hortensius dot net
Reported by: giolaza at gmail dot com
Summary: switch case problem
Status: Open
Type: Bug
Package: *General Issues
Operating System: ubuntu
PHP Version: 7.2Git-2019-02-08 (Git)
Block user comment: N
Private report: N
New Comment:
This is not unexpected - but you are obfuscating your code through the switch{}
Since $var='' and you have a case (in_array($var,$array)) you are basically testing
whether '' == false (result of in_array($var,$array) ), which it is:
var_dump(''==false) returns true. If you want to test multiple cases in this construct you
should use switch(true) - see https://3v4l.org/6B2r8
Previous Comments:
------------------------------------------------------------------------
[2019-02-08 08:04:35] giolaza at gmail dot com
forgot write comment //$var=1;
------------------------------------------------------------------------
[2019-02-08 08:03:45] giolaza at gmail dot com
Description:
------------
wrong case using when I am using in_array in empty var;
if var is not empty, for example - $var=0 all works right.
Test script:
---------------
$array=['users','documents','timePunch','vacation','requests'];
$var=1;
$var='';
var_dump($var);
echo PHP_EOL;
switch($var){
case (in_array($var,$array)):
echo 'CASE IS TRUE';
break;
default:
echo 'CASE IS DEFAULT';
break;
}
echo PHP_EOL;
var_dump(in_array($var,$array));
Expected result:
----------------
string(0) ""
CASE IS DEFAULT
bool(false)
Actual result:
--------------
string(0) ""
CASE IS TRUE
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77584&edit=1
Thread (5 messages)