Recently when reviewing code I stumbled upon $var == "some string" in code, and that sparked the discussion wether you should have strict comparison even for strings.

<?php

// Don't spoil your self, have a guess what weird idea I had to achieve this.
// The solution start at line 42.
//
// Idea by: Christoph Daum
// https://christoph-daum.de

$value = get_customer();

echo 'Loose comparison is: ';
if ( $value == 'Hans Sampleman' ) {
	echo 'True' . PHP_EOL;
} else {
	echo 'False' . PHP_EOL;
}

echo 'Strict comparison is: ';
if ( $value === 'Hans Sampleman' ) {
	echo 'True' . PHP_EOL;
} else {
	echo 'False' . PHP_EOL;
}

echo 'Explicit comparison is: ';
if ( (string) $value === 'Hans Sampleman' ) {
	echo 'True' . PHP_EOL;
} else {
	echo 'False' . PHP_EOL;
}

/*
The code will result in:

Loose comparison is: True
Strict comparison is: False
Explicit comparison is: True
*/

I’ve created proof of concept to show that it actually might be beneficial to be strict, even for strings. While this PoC is crafted and very unlikely to happen in reality, it proofs that you want to be strict.

The magic happens in get_customer() which is missing in the excerpt by design. Feel free to take a quiz and try to guess what happens inside. Feel free to comment if you figured it out without spoiling it in the comments.

This is the complete code example.