mardi 1 mai 2018

PHP Returning Data Type with Switch vs If Else

I'm trying to track down why the if/else logic returns the correct datatypes versus the switch that does not.

IF/ELSE:

$value = false;
var_dump($value);

if(is_int($value)) {
  echo "INT:".$value;
} elseif (is_bool($value)) {
  echo "BOOL";
} elseif (is_null($value)) {
  echo "NULL";
} else {
  echo "DEFAULT";
}

SWITCH:

$value = false;
var_dump($value);

switch ($value) {
  case is_int($value):
    echo "INT";
    break;        
  case is_bool($value):
    echo "BOOL";
    break;
  case is_null($value):
    echo "NULL";
    break;
  default:
    echo "DEFAULT";
}

I'm not using strict comparison in the if/else. Not sure what's going on. Anyone?

Aucun commentaire:

Enregistrer un commentaire