Why do we have the dollar sign before every php variable?

When should I use double equals sign instead of triple equals sign in PHP?

  • I know triple equals sign also compare types of variables. But, What's the rationale beyond this? Where to use double equals sign?

  • Answer:

    $a == $b if $a and $b are equivalent. 3.0 == 3 is true. $a === $b if $a and $b are identical. (Type and value are the same). 3.0 === 3 is false. Both == and === have their unique uses. Sometimes you need to use ==: if(100 == "1e2") print "Answer is correct!"; Sometimes using == will give you the wrong answer. if(strpos('tree', 't') == false) print "Tree does not contain the character t."; This will give you an absurd answer because 0 is equivalent to false. To avoid such errors use ===.

Edward Yu at Quora Visit the source

Was this solution helpful to you?

Other answers

The difference between == and === is simple. == compares the content of the variables to see if they are same; if $A =12345 and $B = 12345 > == will be true. also if $A = 12345 and  $B= "12345" > == will be true as it is independent of the type  of variable and only compare the content. On the other hand === will return true in first case as both are integers and has equal value but false on second as even though both values are same, one is integer and other is string. Update : The double = sign is used like in cases where you want to compare just the values like when for some reason you accept a mobile number as string and need to compare with the number in database that you have stored as integer. without having to use int($xxx) or char($xxx)

Blaise M Crowly

You should avoid double equals signs at all costs. Edit... reasoning: A double equals sign comparison is never necessary.  It's better to be explicit about what you're checking rather than vague.  Being explicit in code lends to fewer mistakes and increased readability. For example, suppose you're writing a cookie library, and you make a function to determine whether a cookie's value has been set.  Maybe that function checks whether $cookie == null.  Then later, some other developer decides to store a path in a cookie.  Maybe one of the pages calls the library function to see if a cookie is unset, and if it is, it defaults the path to '/root'.  Maybe an empty string if a valid cookie value.  Now, if the cookie ever gets set to an empty string, the cookie library will think the cookie hasn't been set, and will default it to '/root'... making it impossible to actually persist an empty path as a cookie value.  What a mess.  You can avoid this hassle by being explicit in your equality checks. There is no reason to rely on double equals.  It's just laziness.

John Kurlak

I never needed to use the triple-sign as a matter of fact. Since PHP is a weak typed language, you can use them in any way you like, or just treat everything as a string if that suits you. More of weak typing here: http://en.wikipedia.org/wiki/Strong_and_weak_typing Only in very specific situations you would need to know if a variable was defined or has been cast into a specific type. In 99 out 100 times, you'll only need to use and compare its values.

Christian Dechery

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.