0

So ive searched around and cant find the answer what im trying to do is find out if a variable is equal to another variable in exact characters.

<?php
$a = 'aBcD';
$b = 'abcd';
if($a == $b){
echo 'yep';
} else {
echo 'nope';
}
//variables above are not equal to each other
?>

aBcD and abcd are not equal but no matter what i try it always comes up as true. Im sure theres an easy way to find this out but i cant seem to find it. can someone help me. Thanks

pavel
  • 26,538
  • 10
  • 45
  • 61

3 Answers3

2

If you want to compare two case-insensitive strings, transform both using strtolower.

if (strtolower($a) === strtolower($b)) {
    // string are equal
}

See that I used === to avoid situation when 1 == '1a' - I find two identical case-insensitive strings.

pavel
  • 26,538
  • 10
  • 45
  • 61
  • hey, ill explain it abit more and what its for. its basically checking against the database for XBOX gamertags and you know you can have similar lookin GT for exmaple. ThePro and thepro. They are 2 different users but my script sees them both as the same person. so if a user wants to add their GT their and it has the same charascters as somone else they cant – user3833066 May 25 '15 at 14:44
  • @user3833066: no problem, I understood your question. Just use what I wrote above, ideally with `===`, because in PHP `1 == '1a'`, but `1 !== '1'`. – pavel May 25 '15 at 14:46
  • its still doing the same as before ): – user3833066 May 25 '15 at 14:59
  • @user3833066: no, it works. Maybe you implemented it bad, but I don't know what you're doing with it. It works, don;t worry. Now it's up to you :-) – pavel May 25 '15 at 15:01
  • the issue was down to collation – user3833066 May 25 '15 at 15:57
1

== is case sensitive. Check for strcasecmp

var_dump(strcasecmp('aBcD', 'abcd') == 0); // true
Federkun
  • 36,084
  • 8
  • 78
  • 90
0

<?php
    $a = 'aBcD';
    $b = 'abcd';
    if(strtolower($a) == strtolower($b)){
    echo 'yep';
    } else {
    echo 'nope';
    }
    //variables above are not equal to each other
?>

Use strtolower function to convert all string to lower and then check condition.

Ananta Prasad
  • 3,655
  • 3
  • 23
  • 35