views:

397

answers:

5

Hi,

First, I tried searching for different variations of the title I've put for this question, both in StackOverflow and google. I couldn't find a solution.

I am fairly new to php. New enough to not know the difference between using eq and == for string comparison!

I usually use == to compare strings in PHP. I am comfortable with it.

But I've seen code using eq to compare strings. And I vaguely remember someone making an observation like 'oh! I used == to compare strings. I should have used eq'

I just want to know whether using == is okay to do simple string comparisons? I am not talking about special cases, case-sensitive, substring or any fancy type of string comparison. Just checking whether apple is the same as apple.

Is == enough? or should I use eq.


EDIT:

my mistake :( thanks a lot for clearing my 'not-well-researched' doubt! it must have been Perl. I got confused seeing the code embedded inside HTML and thought it was a different way of embedding PHP. sorry.

+12  A: 

There is no eq operator in PHP. There is however == and ===.

=== is a strict comparison operator and won't do type conversion.

== will do type conversion (for example '' == 0 evaluates to true).

See Comparison Operators for a full list and Type Juggling for the rules of PHP type conversion. The only reference I could find to eq was as an argument to version_compare().

cletus
A: 

eq in PHP would be news to me but there is the strict comparison operator

$a === $b

check the manual for details.

Pekka
"binary comparison"?
troelskn
I assume he means "binary" in the sense that it takes too argument or has two results (compared to, say, the Perl cmp operator) but the word "binary" has some unfortunate ambiguities in this context (eg bitwise vs logical comparison).
cletus
My mistake, I meant "strict". Revised.
Pekka
+5  A: 

To accompany my comments: The eq operator in Perl tests for string equality, while the == tests for numerical equality only.

Even though PHP started once as a collection of Perl scripts, I don't think, they have copied this operator and this page seems to agree.

Boldewyn
+3  A: 

PHP does't have an eq operator. You're probably thinking of perl, where eq will compare two variables as strings.

PHP has an equality operator (==) and a true equality operator (===). The true equality operator (===) will test that the expressions on each side of the operator are both equal, and of the same type. The equality operator (==) will attempt to coerce each expression to the same type, and then compare them.

//this is true
'45' == 45

//this is false
'45' === 45

There are some cases where == will make bad guesses when it comes to types, do if you know you have tow expressions of the same type, it's best to use ===.

Alan Storm
A: 

The == operator checks if there's a perfect match between two variables, literals or a combination of these two programmatic entity ... but is not type safe!

If you use it to compare strings, you are comparing a string with a regular expression to find a perfect match!

But there are a lot of most powerful way to compare strings in PHP, you need only know what you are trying to accomplish .... for example: similar_text(), strcasecmp(), strcmp(), etc. Or you can compare strings by using regular expressions by calling one of the functions ereg(), eregi() with your own pattern!

BitDrink