Rowan
Rowan

Reputation: 485

Case-insensitive statements?

Please forgive the ignorance, but I'm very new to PHP and I couldn't find what I want out there anywhere. So I've com here, where I know I'll get an answer.

What I want to do is accommodate for any letter case inconsistencies in the data I am pulling in (which I didn't create).

For example I would like the following if statement to work even if the $var is written as 'hello', 'HELLO', 'Hello', 'HeLlo', 'hELLo', 'hElLo',.. etc etc.

if ($var == 'hello') {
 echo 'It works!';
}

Obviously that won't, but what could I do to that to make it not worry about the case of the text?

I have had a look around for these kinds of things, I found strcasecmp(), but to me that doesn't seem to be exactly what I want.

Upvotes: 0

Views: 85

Answers (3)

Dan Blows
Dan Blows

Reputation: 21174

There's a dedicated PHP function for this - strcasecmp($str1, $str2). However, I would still go with the strtolower($var) because you don't have to worry about unicode.

Upvotes: 1

D_V
D_V

Reputation: 416

Use strtolower

eg:

$var = 'HeLLo';
if (strtolower($var) == 'hello') {
 echo "it works";
}

Upvotes: 1

Naftali
Naftali

Reputation: 146302

Why don't you try:

if (strtolower($var) == 'hello') {
 echo 'It works!';
}

So if $var = 'HelLo';, it will put it to lower case

Upvotes: 6

Related Questions