Comparison with int and string in IF condition

Code in PHP:

<?php $key = 0; if($key == 'monty' && $key == 'anil'){ echo 'Mackraja'; }else{ echo "Nothing"; } ?> 

OR

 <?php $key = 2; if($key == '2abc' || $key == 'abc2'){ echo 'Mackraja'; }else{ echo "Nothing"; } ?> 

EXIT : Mackraja

+5
source share
7 answers

After a long discussion with my colleagues: We found a solution

Examples:

 #1 var_dump("00005ab" == 5); // true #2 var_dump("abc" == 0); // true #3 var_dump(2 == "ab2"); // false #4 var_dump(2 == "2ab"); // true 

Explanation:

Provided that the left operand (2) matches the correct operand (ab2), if it is not found at the beginning, then the output will be false else true.

 #1 : we found 5 at the begining output will be true. 0 will be eliminated. #2 : string + int = int output will be true. Priority of int is higher, 0 == 0 output will be true, #3 : 2 is not found at the beginning of match value (ab2), output will be false. #4 : 2 is found at the beginning of match value (2ab), output will be true. 

Cause:

 1.) In PHP, its automatically convert Type Casting. 2.) Rules of Type Casting : 2.1) int + int = int 2.2) string + string = string 2.3) float + float = double 2.4) int + string = int 2.5) float + string = double 2.6) int + float = double 
+1
source

If you are comparing a number with a string or the comparison includes numeric strings, each string is converted to a number and the comparison is performed numerically. These rules also apply to the switch statement. - Source

It means that

 <?php var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true var_dump("10" == "1e1"); // 10 == 10 -> true var_dump(100 == "1e2"); // 100 == 100 -> true 

So, when you compare $key (with a value of 0 ) with strings, the strings are 0 , and both of them return true. This way he will always output "Mackraja".

Changing the value of $key to any other integer will return false.


note that

Type conversion does not occur if the comparison === or !== , since this involves a comparison of type and value. - Source

This means that changing the comparison operator to === will mean that the values ​​must match exactly - i.e. line 1 will not be equal to the integer 1: they must correspond to the formats:

 echo $key == 'monty' && $key == 'anil' ? 'Mackraja' : 'Nothing'; //will echo "Mackraja" echo $key === 'monty' && $key === 'anil' ? 'Mackraja' : 'Nothing'; //will echo "Nothing" 
+9
source

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; //0 

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' . '&nbsp;Equal';//does not output }else{ echo '<br/>' . $key . '== 2a2' . '&nbsp;Not Equal'; //outputs } 

and

 $key = 200; if($key == '2e2' || $key == 'abc2'){ echo '<br/>' . $key . '== 2e2' . '&nbsp;Equal';//outputs Now it does not convert it to integer, it converts it to }else{ echo '<br/>' . $key . '== 2e2' . "&nbsp;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: -

enter image description here

+3
source

check type conversion rules for free comparison (Loose comparisons with ==): php type conversion rules

You will see that when using free type comparison, the integer 0 is equal to any string ...

+2
source

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. These rules also apply to switch expression. Type conversion does not occur when the comparison is === or! ==, since this involves comparing the type as well as value.When you provide $ key as 0, then the comparison will be performed numerically, i.e. (int)'monty' = 0 and (int)'anil' = 0 .

Try it if you want the right result.

 $key = 0; if($key === 'monty' && $key === 'anil'){ echo 'Mackraja'; }else{ echo "Nothing"; } 
+2
source

if the string is compared with a number, the string will be converted to the number t, and the check / comparison is performed numerically

if you still need to get the correct output with data type checking, you can use "===" if, as shown below, the result u expects as "Nothing"

 <?php $key = 0; if($key === 'monty' && $key === 'anil'){ echo 'Mackraja'; }else{ echo "Nothing"; } ?> 

this will output: Nothing

+2
source

Just do it like this ..

 <?php $key = 0; if((string)$key == 'monty' && (string)$key == 'anil'){ echo 'Mackraja'; }else{ echo "Nothing"; } ?> 

Output:

 Nothing 
+1
source

Source: https://habr.com/ru/post/1242175/


All Articles