Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pickab00's avatar

Difference between !== & !=

So I recently encountered an error with when I had !==, it returns to true. Say for an example, if 4 !== “5” it becomes false on my live server. But If I use just st it works as intended.

Does !== mean that the type should also not match? I mean an Int and a String. Where as for !=, it matters only for the value and it does not care about the type. Am I on the right page?

So what is the difference? Also I should note that !== works perfect on my local development.

0 likes
23 replies
bestmomo's avatar

From manual :

$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.

2 likes
pickab00's avatar

Yes that is what I said above. Basically if it was a string and an int, it will be true. But in this case they both are int's. 5 !== 4 is false. But 5 !== "4" is true because the types is not equal to each other.

Snapey's avatar

5!==4 is always true ?

>>> 5 !== 4
=> true
>>> 5 !== '4'
=> true
>>> 5+0 !== 4
=> true
>>> 5+0 !== '4'
=> true

Don't know why you would ever get false?

1 like
jlrdw's avatar

I have never seen

!===

Just

<>  // or  !=

Curious, where did you get that in a reference.

From php manual

$a != $b Not equal TRUE if $a is not equal to $b after type juggling.

$a <> $b Not equal TRUE if $a is not equal to $b after type juggling.

$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.

Where is the reference to

!===
Cronix's avatar

I think that is a typo in the OP. There is no such thing as a ! followed by 3 = signs in php. You'll get an error, so it couldn't have been working for him.

>>> 5 !=== 4
PHP Parse error: Syntax error, unexpected '=' on line 1
1 like
jlrdw's avatar

Ah, then above reference explains. Remember php loose typed, c, c++, c# not forgiving like php. Funny ain't it, php is written in C.

pickab00's avatar

Guys I’m so sorry. That was a typo. I meant !==.

@Snapey 4 !== 5 will return false won’t it? But 4 != 5 will return true. Because of the type difference. 4 is not equal to 5 is correct. But also it is false because int is equal to int. Hope I am making sense here. 4 !== 5. Both are integers. However 4 !== “5” is true. “5” is a string. So they are not equal in anyway.

@jlrdw Yes it was a typo and you are absolutely correct.

@Cronix Yes it was a typo. It is !== and !=

So to wrap things up, !== is same as === and != is the equivalent of ==. !== will return true only if the values AND types are different. Where as != will be true if the value is different and does not care about the types.

jlrdw's avatar

If one of us gave answer that helped, can you mark as answered.

pickab00's avatar

I’m not sure what to mark. I kind of explained it on the OP too

Snapey's avatar

@Snapey 4 !== 5 will return false won’t it?

NO?

4 is definitely not equal to 5 whatever type it is, so will always be true.

4 !== '4' would be true because although they are the same value they are not the same type

1 like
pickab00's avatar

@Snapey My server does not except this. 4 !== 5 returns false because of the type difference. I am on laravel 5.6, PHP 7.2.10. The only difference is that I have my MySQL Native Driver disabled. So if I am taking a value from db and comparing it with some other value like the example above, it returns false. But 4 != 5 returns true.

I think It has to do with MySQLnd (On my server I mean)

Snapey's avatar

You aren't putting this in the SQL query are you? because there it would be '<>' and there is no such thing as comparing objects.

4!==5 would not return false because of a type difference.

1 like
NOMGUY's avatar

In != we check is it "not equal to" example: 2 !=4 .. it will give true. and in !== we check for value as well as type. example: 2 !== 4 .. it will give true. but a !== 2 it will give false as "a" is a string and 2 is an integer.

Snapey's avatar

@NOMGUY

but a !== 2 it will give false as "a" is a string and 2 is an integer.

NO?

string 'a' is not equal to 2 so the response is true

Is it just me or has the world gone mad?

Snapey's avatar
Snapey
Best Answer
Level 122

Just for completeness

>>> 4 != '4'
=> false                // because they are loosely the same
>>> 4 !== '4'
=> true                // because they are not the same type even though they are loosely the same
pickab00's avatar

Yes it must have been a typo on his side.

It has indeed gone wrong xD. I just wanted to confirm that my OP was correct. And look where it got us xD

pickab00's avatar

Yes @Snapey that is what I was trying to say since last night! I just wanted to confirm this. Anyways. Thanks guys!

D9705996's avatar

As @Snapey and everyone else has been saying is that when using strict comparison like === and !== the type and the value are evaluated. It might be easier to thing about === which says both type and value must be equivalent

>>> "4" === 4
=> false
>>> "5" === "4"
=> false
>>> "5" === "5"
=> true

The !== result will be the opposite of ===

>>> "4" !== 4
=> true
>>> "5" !== "4"
=> true
>>> "5" !== "5"
=> false
Cronix's avatar

Perhaps my SQL Native Driver needs to be enabled and updated

What does this have to do with sql? We're talking about php here.

Are you talking about something like this?

SomeModel::where('someColumn', '!==', 'someValue')

?

If so, that's not php. It's using php to construct sql. "!==" is not valid in sql. It would just be "!=" or "<>".

https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html

Snapey's avatar

@pickab00 I would like to see a test case that shows that your original question is correct and that 4 !== 5 can really return false.

There is no way something as fundamental as this should behave differently on one machine compared to another

pickab00's avatar

I drifted off. Edited. This has been resolved. So many times by so many people. RESOLVED!

Cronix's avatar

If it's solved, maybe mark it solved? I still don't know why you mention the sql driver or what you think it has to do with your problem? We're just trying to make sure you're clear because you are mentioning things that don't seem to or shouldn't have anything to do with it.

Please or to participate in this conversation.