Code for making an if statement case insensitive

Hi there, I am reading the book phpmysql and have a question to if statements. I like to know how I can make an if statement case insensitive

The below example shows an if statement, so that if the condition “Kevin” is true, the sentence “Welcome to our website!” is displayed. Now I like to know how I need to modify the code that if “Kevin”, “kevin”, “keVin”, “keviN” etc… is entered the condition becomes true without putting all combinations into the formula.

the if statement is:

$name = $_REQUEST[‘name’];
if ($name == ‘Kevin’)
{
echo ‘Welcome to our website!’;
}

thanks for your help in advance!

If you’re fairly new, you should probably avoid getting into regex for now (it’s like a whole other language, even a lot of advanced PHPers have trouble with it) and finish working your way through the book first.

The easiest way would be to use strtolower() to change the supplied input to lowercase and test against the desired lowercase string.

When working with case sensitive you can also return the capital after testing.

$name = $_REQUEST[‘name’];

if(strtolower($name) == “kevin”){
echo 'Welcome to our website '.ucfirst($name);
}

Thanks for your suggestions and posting the code. Well understood and works for my needs perfectly.

When working with case sensitive you can also return the capital after testing.
Sorry to correct you Exphor but your post is a bit misleading here. The call to strrolower() does not alter the variable passed to it as an argument so, in the previous example, $name is lowercased only for the purpose of the if. As a secondary point some users are fussy about how their usernames—and presumably their given names too—are capitalized so it is generally better just to display them back exactly as they were given. Err, that is after you’ve called htmlspecialchars() to prevent an XSS vulnerability. So here’s how I would do it:

$name = $_REQUEST['name'];

if (strToLower($name) == "kevin") {
   echo 'Welcome to our website '. htmlSpecialChars($name);
}