SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,257
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    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.

  3. #3
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);
    }

  4. #4
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your suggestions and posting the code. Well understood and works for my needs perfectly.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $name $_REQUEST['name'];

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



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •