6

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";
    }
?>

OUTPUT: Mackraja

Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
Monty
  • 1,110
  • 7
  • 15
  • 4
    why use `&&` ? one attribute cant get two different values at the same time! use `||` –  Feb 03 '16 at 09:46
  • it does not mean which operator we are using || or && it giving same output – Monty Feb 03 '16 at 09:47
  • logical, its not true , not about syntax –  Feb 03 '16 at 09:49
  • yes, i was thinking same logically its not true but how its coming in true condition @MohsenShakibafar – Monty Feb 03 '16 at 09:51
  • because of type. type difference 0 is integer , if you put `'0'` you see it work fine. see http://stackoverflow.com/questions/523643/difference-between-and-in-javascript –  Feb 03 '16 at 10:00
  • Funny, because when comparing correctly the first condition will never be true – scrowler Feb 03 '16 at 10:24
  • See my answer, i hope it will explain you more and will help you in your programming – Monty Feb 03 '16 at 11:30
  • it's look like the first one , all cause of ,different types –  Feb 04 '16 at 06:40
  • @MohsenShakibafar No, its not like that, even i write answer for this, i hope it will help others – Monty Feb 04 '16 at 06:41
  • @Monty , you right , my mistake. –  Feb 04 '16 at 06:43

7 Answers7

10

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. These rules also apply to the switch statement. - Source

This 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're comparing $key (with value 0) to the strings, the strings have value 0 and they both return true. This way, it will always output "Mackraja".

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


Note that

The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value. - Source

This means that changing the comparison operator to === will mean that the values have to match completely - i.e., a string 1 will not be equal to an integer 1: they have to match formats:

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

echo $key === 'monty' && $key === 'anil' ? 'Mackraja' : 'Nothing';
//will echo "Nothing"
Ben
  • 8,894
  • 7
  • 44
  • 80
  • When we do it :---> echo $key == 'monty' && $key == 'anil' ? 'Mackraja'; 'Nothing'; //will echo "Mackraja" :-> It will give you Nothing not Mackraja echo $key === 'monty' && $key === 'anil' ? 'Mackraja'; 'Nothing'; //will echo "Nothing" – Monty Feb 03 '16 at 10:29
  • Your answer not satisfying me – Monty Feb 03 '16 at 10:33
  • @Monty Apologies there was a syntax error in my `if/else` statement. Try now. – Ben Feb 03 '16 at 10:34
  • See my answer : @Ben – Monty Feb 03 '16 at 11:29
4

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' . '&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 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:-

enter image description here

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • I wanted to know the reason, i know how to do it. Not satisfy with your answer – Monty Feb 03 '16 at 12:06
  • @Monty I have edited my answer, I hope I am able to give you better idea this time. – Prafulla Kumar Sahu Feb 03 '16 at 12:29
  • @Monty I have added more explanation with more example where you can notice different condition and behavior of PHP. – Prafulla Kumar Sahu Feb 04 '16 at 07:42
  • : I am impressed with the reference link which you written in your answer. Have you look my answer, what you think its near to real answer or not – Monty Feb 04 '16 at 08:28
  • @Monty now going to read other answers , I have not gone through any other answers still now. – Prafulla Kumar Sahu Feb 04 '16 at 08:30
  • @Monty yes, I can say you have given a good language to explain the concept and I have provided better examples :P why reference link is better is next person will go through all the things you have gone when he will visit this page and these links are going to remain for a long time with php manual . – Prafulla Kumar Sahu Feb 04 '16 at 08:33
  • Thanks, So as we both know what is reason behind it, I hope if any developer come here than he will not confuse. – Monty Feb 04 '16 at 08:39
  • @Monty there is less chance in current time developers will like to spend time on core, everyone wants to earn money by learning quickly to make things work either correctly or semi-correctly . :D – Prafulla Kumar Sahu Feb 04 '16 at 08:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102564/discussion-between-monty-and-prafulla-kumar-sahu). – Monty Feb 04 '16 at 08:43
  • how to compare 9829098290 and 09829098290? Kindly share your suggestion. Thanks. Note: please do not suggest to add single / double code outside of both the numbers. Any standard method? – Kamlesh Oct 17 '21 at 10:20
  • @Kamlesh I need to understand how you want to compare them as integers or string or how exactly you want to compare them, based on that only the comparison can happen. and it will be better if you can post this as a separate question, so that we can handle that separately without messing it. – Prafulla Kumar Sahu Oct 18 '21 at 13:12
2

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

You'll see that with using loose type comparison, integer 0 equals to any string...

ejuhjav
  • 2,660
  • 2
  • 21
  • 32
2

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. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.When you provide $key as 0 then comparison will take place numerically ie, (int)'monty' = 0 and (int)'anil' = 0.

Try this if you want correct result

$key = 0;
if($key === 'monty' && $key === 'anil'){
    echo 'Mackraja';
}else{
    echo "Nothing";
}
Ninju
  • 2,522
  • 2
  • 15
  • 21
2

if a string is compared with number, string will be converted t number and the check / comparition is done numerically

if you still need to get the correct output with data type check you can use "===" in if as below will result the output u expect as "Nothing"

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

this will output: Nothing

1

Just do like this..

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

Output:

Nothing
Amit Rajput
  • 2,061
  • 1
  • 9
  • 27
1

After a long discussion with my colleagues : We Found 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 :

In the condition left operand (2) match with right operand (ab2), if it does not found at the beginning then 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.

Reason :

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
Monty
  • 1,110
  • 7
  • 15