!= and !== both not always working? [closed]

This is actually not really a WordPress issue, but pure PHP.

  • != compares two value and return true when the two values are not like the other. 0 != '0' will return false because the values are like each other, the one being an integer value 0 and the other a string value of 0

  • !== compare two values and return true if two values are not identical. 0 != '0' will return true because the two value are not identical. 0 is an integer and '0' is string, so they are clearly not identical. It it the same as an apple, a green apple is not identical to a red apple because of colour

In any condition you need to know what the value will be you need to do a comparison. Ideally you would want to do strict comparisons, that is !== and ===, but for this to work you would need know exactly what you value would have to be and of what kind.

As I showed already, 0 can be a string or an integer. If you are going to accept 0 as an integer or a string, then you should use less strict rules (!= or ==). If you just need to accept 0 as an integer OR a string, then you should use strict rules (!== or ===)

EDIT

For stict rules, you can always cast a value to what you need it to be before comparison, like you can cast a string value of 0 to an integer and then do a strick compare between the two values, so in short, 0 !== (int) '0' should return false as the values would be identical as '0' will become 0 when being cast to an integer value