I searched on StackOverflow and Google and I can’t find the answer to this question:
Should we always use the triple equal in PHP for validation?
For example, I have a variable:
$x = '1';
if($x == 1) // will work
if($x === 1) // will not
Now, my point is if we need to validate numeric fields like:
if(is_numeric($x) && $x == '1') { will be the equivalent to if($x === 1)
Since === also validate the type, will it be better if we always use the ===?
It depends entirely on the script you’re writing, there’s not one correct answer for this. Having said that, there aren’t many situations where you don’t already know the type of the variable (except perhaps user input).
This is the reason I stick to using
==and only use===when there could be more than one type of the variable.The
==is fine most of the time, it wouldn’t have been invented if you weren’t supposed to use it 🙂