PHP: Stop using the identical comparison operator everywhere

This is more a note to self post. But I thought maybe it is beneficial for others as well. The identical comparison operator or three equal signs, is so deep in my muscle memory and vision that for a long time I didn't even notice typing that third equal sign, or bat an eye when it is there. But these last weeks I'm becoming more and more aware of the uselessness of the type check that is build-in the identical comparison operator. In the good old days I would write something like function isEqual($a, $b) { return $a === $b; } But I'm using type hinting more and more. So that code gets rewritten as function isEqual(string $a, string $b) { return $a == $b; } The type check has moved to the arguments. And even with functions that have multiple return types, it makes no sense anymore. Even the official documentation has examples like this, for instance the strpos function. $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme); // Note our use of ===. Simply == would not work as expected // because the position of 'a' was the 0th (first) character. if ($pos === false) { // do something } The check could be replaced by is_bool($pos). Now that I'm aware of this ingrained behavior, I'm discovering a lot of places where I have why-did-i-do-that moments. The identical comparison operator is not obsolete, but start questioning the usage in the code.

Apr 6, 2025 - 18:06
 0
PHP: Stop using the identical comparison operator everywhere

This is more a note to self post. But I thought maybe it is beneficial for others as well.

The identical comparison operator or three equal signs, is so deep in my muscle memory and vision that for a long time I didn't even notice typing that third equal sign, or bat an eye when it is there.

But these last weeks I'm becoming more and more aware of the uselessness of the type check that is build-in the identical comparison operator.

In the good old days I would write something like

function isEqual($a, $b) 
{
    return $a === $b;
}

But I'm using type hinting more and more. So that code gets rewritten as

function isEqual(string $a, string $b)
{
   return $a == $b;
}

The type check has moved to the arguments.

And even with functions that have multiple return types, it makes no sense anymore. Even the official documentation has examples like this, for instance the strpos function.

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
// do something
}

The check could be replaced by is_bool($pos).

Now that I'm aware of this ingrained behavior, I'm discovering a lot of places where I have why-did-i-do-that moments.

The identical comparison operator is not obsolete, but start questioning the usage in the code.