Bug #77584 [Csd->Nab]: switch case problem

From: Date: Fri, 08 Feb 2019 18:11:17 +0000
Subject: Bug #77584 [Csd->Nab]: switch case problem
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219446@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77584&edit=1 ID: 77584 Updated by: requinix@php.net Reported by: giolaza at gmail dot com Summary: switch case problem -Status: Closed +Status: Not a bug Type: Bug Package: *General Issues Operating System: ubuntu PHP Version: 7.2Git-2019-02-08 (Git) Block user comment: N Private report: N Previous Comments: ------------------------------------------------------------------------ [2019-02-08 09:37:14] giolaza at gmail dot com Thanks for answer ------------------------------------------------------------------------ [2019-02-08 09:27:08] sjon at hortensius dot net 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 ------------------------------------------------------------------------ [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

« previous php.bugs (#219446) next »