henrywright
henrywright

Reputation: 10240

How can I make my switch statement case insensitive?

In the following example, sometimes $var can be "Value", "value" or even "VALUE".

switch ( $var ) {
    case "value":
        // Value and VALUE don't seem to match here.
        break;
}

The comparison seems to be case sensitive (only the all-lowercase "value" matches). Is there a way to perform a case-insensitive comparison?

Ref: http://php.net/manual/en/control-structures.switch.php

Upvotes: 3

Views: 1927

Answers (2)

Dan
Dan

Reputation: 11084

Convert your string to lowercase, then compare it to all lowercase strings

switch ( strtolower($var) ) {
    case "value":
        // Value and VALUE don't seem to match here.
        break;
}

Upvotes: 3

Lucarnosky
Lucarnosky

Reputation: 514

$var = strtolower($var)

And then in the switch cases write all lowercase

Upvotes: 2

Related Questions