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

Issue

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.

Solution

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.

Answered By – Boldewyn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published