If you are comparing a number with a string or comparing numeric strings, then each string is converted to a number and the comparison is performed numerically. Link
Now check
<?php $key = 0; if($key === 'monty' && $key === 'anil'){ echo 'Mackraja'; }else{ echo "Nothing"; } ?>
you will get the output "Nothing".
now the integer value of these lines is 0, you can notice it here.
<?php $a = 'monty'; echo (int)$a;
How why?
Taking a situation from programming a bit to explain this, how best to publish the best way, I am trying to explain this concept to you.
If you are faced with the principle of homogeneity, it says that you cannot compare two quantities if their sizes are different.
Here, in programming, you cannot directly compare a string and an integer; it must be converted to whatever you want to compare.
In PHP, if you go through the manual, you may notice or is this the main idea of ββPHP, which: -
PHP does not require (or support) an explicit type definition in a variable expression; the type of the variable is determined by the context in which the variable is used.
For the comparison operator, when you even compare two strings (with free comparison), they are converted to integers and then compared. You may notice this on the "Comparison with various types" link page ..
So, I can say that the comparison operator requires an integer variable of type to comapre, so both operators are compared with 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 value of a variable from the variable in question, as the variable changes.
I give you another clean table, which you have already seen from the previous link, how various types of variables are thrown into PHP when comparing, you can notice this from here.
If you need more resources, the best idea is to find the source code, how it is written, you can learn a lot from this, and why they like it, it's better to answer the PHP group.
Maybe you can get something like that which could not arise due to such a situation, if PHP will not convert this variable implicitly, if you want your PHP to behave like this, you can make your own version.
I add a table of how the conversion occurs when comparing a free variable: -
