Is PHP variable value case sensitive

Discussion in 'PHP' started by ketting00, Dec 15, 2015.

  1. #1
    A website I develop currently have a section that allows users to name things, but I don't want they give any names to anything. So I restrict words which they could use. My current code look like this:
    
    $given_name = trim($_POST['given_name']);
    
    $reserve = array(
            .......
            "demo" => "demo",
            "Demo" => "Demo",
            "example" => "example",
            "Example" => "Example",
            .......
    );
    
    if (in_array($given_name,$reserve)) {
            die(msg(0,"[ ".$given_name." ] is unavailable."));
    }
    
    Code (markup):
    The list is long and I don't think I will be able to list everything I don't want the users to use.

    Can I just do it like this, is it valid:
    
    $reserve = array(
            .......
            "demo" => "demo",
            "example" => "example",
            .......
    );
    
    Code (markup):
    Thank you,
     
    ketting00, Dec 15, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    keys should be case sensitive... though I'm not quite sure what the blazes you are setting associative keys for since you're using in_array.

    Which in_array is case sensitive. Honestly this seems more like a job for a regular expression or stripos. The latter would be ideal as then it's just check for a non boolean false.

    $reservedWords = [
    	"demo",
    	"example"
    ];
    
    foreach ($reservedWords as $word) {
    	if (stripos($given_name, $word) !== false) die('your message here');
    }
    Code (markup):
     
    deathshadow, Dec 15, 2015 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Thanks for quick response.
    It comes in time needed.

    And thanks for suggestion. I'll further study about it.
     
    ketting00, Dec 15, 2015 IP