Is there any way in php to make the SWITCH opreator compare cases strict?

I have such a management structure:

switch ($var) {
   case TRUE: 
   break;

   case FALSE:
   break;

   case NULL:
   break;
}

And my case is NULLnever called because, as I found in the php manual:

Note that switch / case makes a free comparison .

I know what I can use IFinstead SWITCH, but I would not want to, I already have IF'sin each CASEmine SWITCH.

Is there any way to rewrite SWITCHor use some tricks to compare it with strings?

+5
source share
2 answers

Yes you can do

switch (true) {
   case $var === TRUE: 
   break;

   case $var === FALSE:
   break;

   case $var === NULL:
   break;
}
+7
source

, , .

, , - PHP ; "PHP" ( - , "PHP" )

0

All Articles