当PHP中的变量看起来相等时2变量如何不相等?

I have eperienced a very strange issue and I can't get my head around it.

I actually have 2 value which PHP says are NOT equal, when they do equal in my eyes.

I have 2 variables:

$pattern = array('/index/' => 'index');
$uri = $_SERVER['REQUEST_URI'];

$array_key = array_keys($pattern);

echo $array_key[0];
echo $uri;

Actually, when I echo both of these, I get the exact same text in my browser:

/index/

However if I do the following:

if ($array_key[0] == $uri) {
 echo 'Equals';
} else {
 echo 'Not Equals';
}

It ALWAYS gives me that they do NOT equals.

I do not know why is this, but my mind is kinda messed up right now. Can anyone please help me?

try this

if (trim($array_key[0]) == trim($uri)) {
 echo 'Equals';
} else {
 echo 'Not Equals';
}

If the code says they are not equal, they must be not equal. Change

echo $array_key[0];
echo $uri;

to

echo "X" . $array_key[0] . "X";
echo "X" . $uri . "X";

I am pretty sure you will see difference.

try strcmp($array_key[0],$uri)

In PHP there are kind of null value strings that doesn't print on an echo. See this excample:

http://3v4l.org/qY1au

$val1 has a \0 at the end of the string, but the printed value of var dump doesn't show an empty string at the end. Comparing it to a similar looking string returns false. String length output of var_dump shows that $var1 has more chars than $var2.

Only after using the trim function the value comparation returns true.

You should check if one of your strings contains something like \0.

You should check