|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-02-08 08:03 UTC] 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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 06 09:00:02 2026 UTC |
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