Question

The following code:

switch ($value) {
    case INF: $x = 'INF';
        break;
    case -INF: $x = '-INF';
        break;
    case NAN: $x = 'NaN';
        break;
    default: break;
}

doesn't work as I expected. I know that there are functions like is_infinite() but am I able to check variable infinity inside a switch statement?

My input can be any simple value (i.e. not an array and not an object). Could be integer, float, string, whatever.

Était-ce utile?

La solution

am I able to check variable infinity inside a switch statement?

No. Switch statements work with constants, not with expressions.

if (is_infinite($value) || is_nan($value)) {
  $x = (string)$value;
}

It's less lines of code, too.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top