If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically. Reference
Now check
<?php
$key = 0;
if($key === 'monty' && $key === 'anil'){
echo 'Mackraja';
}else{
echo "Nothing";
}
?>
you will get output "Nothing".
now integer value of those strings are 0 you can observe that here.
<?php
$a = 'monty';
echo (int)$a; //0
How ? / Why ?
Taking the situation to out of programming little bit to explain it, how ever better way can be posted, I am trying my way to explain the concept to you.
If you have come across principle of homogeneity it states that you can not compare two quantity if their dimensions are different.
Here in programming you can not compare a string and integer directly, it needed to be converted to any one to be compared.
In PHP, if you will go through manual you can notice or that is a basic idea of PHP that:-
PHP does not require (or support) explicit type definition in variable
declaration; a variable's type is determined by the context in which
the variable is used.
For comparison operator when you are even comparing two string (with loose comparison) those are converted to integer and then they are compared You can notice it on the reference page "Comparison with Various Types" table.
So, I can say comparison operator itself needs integer type variable
to comapre, that is why both operators are compared to integers.
$key = 2;
if($key == '2abc' || $key == 'abc2'){
echo '<br/>Mackraja';//outputs
}else{
echo '<br/>nothing';//does not output
}
now,
$key = 200;
if($key == '2a2' ){
echo '<br/>' . $key . '== 2a2' . ' Equal';//does not output
}else{
echo '<br/>' . $key . '== 2a2' . ' Not Equal'; //outputs
}
and
$key = 200;
if($key == '2e2' || $key == 'abc2'){
echo '<br/>' . $key . '== 2e2' . ' Equal';//outputs Now it does not convert it to integer, it converts it to
}else{
echo '<br/>' . $key . '== 2e2' . " Not Equal";//does not output
}
now
echo '<br/>'.'1E1'*1; //it outputs 10 why ??
echo '<br/>';
echo '<br/>1E1'*1; //it outputs 10 why ??
see below:-
echo '<br/>cast to int 1E1 = ' . (int)1E1;//10
echo '<br/>Cast to int 1E2 = ' . (int)1E2;//100
//echo '<br />Cast to int 1A1 = ' . (int)1A1;//gives you error as can not be cast
//echo '<br />Cast to int 1A2 = ' . (int)1A2;//gives you error as can not be cast
echo '<br />Cast to int 1A1 = ' . (int)('1A1');//1
echo '<br />Cast to int 1A2 = ' . (int)('1A2');//1
echo '<br/>get int value 1E1 = ' . intval('1E1');//1
echo '<br/>get int value 1E2 = ' . intval('1E2');//1
echo '<br />get int value 1A1 = ' . intval('1A1');//1
echo '<br />get int value 1A2 = ' . intval('1A2');//1
"This is not about getting the intvalue from a variable it is about
how a variable is cast."
I am giving you another clean table, taht you have already seen from my previous reference link,how different types of variable are casted in PHP when compared you can notice that from here.
If you need more resource, the best idea is to go for source code how it is written you can learn a lot from that, and why they did like this can be better answered by PHP group.
May be you may get something like can not becompared for such situation if PHP will not convert these variable implicitly, if you want your PHP to behave like this, you can make your own version .
I am adding the table how conversion happens when loosely variable are compared:-