Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== operator for string [duplicate]

Why does this code echo Yes. even though variables are not equal!

$a = '0e462097431906509019562988736854';
$b = '0e830400451993494058024219903391';

if( $a == $b ) echo 'Yes.';
else echo 'No!';
like image 768
javad naroogheh Avatar asked Mar 09 '15 05:03

javad naroogheh


People also ask

Can we use == operator for strings?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

What does == mean for strings in Java?

Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not.

How do you find duplicate characters in a string C++?

Algorithm. Define a string and take the string as input form the user. Two loops will be used to find the duplicate characters. Outer loop will be used to select a character and then initialize variable count by 1 its inside the outer loop so that the count is updated to 1 for every new character.


2 Answers

Both will treated as numbers, and PHP had limitations in number storage before. So check that.

Try to use '==='. it will check the type also, so those will not convert to numbers.

Refer this question and its answers.

like image 182
arun Avatar answered Sep 22 '22 14:09

arun


You want strcmp, not the equality operator.

like image 42
rhombidodecahedron Avatar answered Sep 22 '22 14:09

rhombidodecahedron