0

I was working on some code made by someone else and came across in if statement that looks like this IF($res === true){ do_something();}

This code is called many times and seems to be working. What is it doing?

I know one equals is assignment and two is comparison. What does the three equals sign operator do?

3 Answers3

2

The easiest way to see it, is that == checks equality and === checks identicality. Equality will check the value, but identicality will check the variable type too.

Examples:

var_dump('true' == true); // bool(true)
var_dump('true' === true); // bool(false)
ollieread
  • 6,018
  • 1
  • 20
  • 36
  • 2
    Your examples are only going to add to the confusion for someone already having difficulty with this subject. Any non-empty string is boolean true but to use 'true' as the example string is just muddying the water. – Shazbot Dec 18 '13 at 02:00
  • Since we're explaining the difference between something being equal and something being identical, using a string called true fits perfectly. Using something other than true would just serve to confuse a user even more as they're not even remotely similar. – ollieread Dec 18 '13 at 11:42
1

This is the "identical" operator. They'll need to be exactly the same to pass the condition.

The 3 different equals

Community
  • 1
  • 1
James Binford
  • 2,753
  • 1
  • 14
  • 12
1

Three === means that result has to be identical. Worded it wrong first time :)

I am Cavic
  • 1,115
  • 1
  • 10
  • 22
  • I don't understand why did someone down vote this? its correct. 3 = signs means that it has to be IDENTICAL. and that is what I wrote I don't see the problem – I am Cavic Dec 18 '13 at 01:57
  • PHP doesn't have 'datatype' nor is it an object comparison, it's actually variable type. It will work with objects, somewhat. – ollieread Dec 18 '13 at 01:57
  • OK maybe I didn't word it correctly but I was trying to say exact same thing that you said it needs to be IDENTICAL. – I am Cavic Dec 18 '13 at 01:58
  • 1
    It was just to get you to change the definition, confusion can be caused by incorrect definition. – ollieread Dec 18 '13 at 01:59
  • I can appreciate that :).. Good choice ! :) – I am Cavic Dec 18 '13 at 02:00