开发者

What's the difference between 'eq' vs '==' in PHP? [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 13 years ago.

开发者_C百科 Improve this question

First, I tried searching for different variations of the title I've put for this question, both on Stack Overflow 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.


My mistake :( thanks a lot for clearing my 'not-well-researched' question! 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.


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().


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.


PHP doesn’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, so if you know you have two expressions of the same type, it's best to use ===.


The == operator checks if there's a perfect match between two variables, literals or a combination of these two programmatic entity ... but it 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!


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

$a === $b

check the manual for details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜