4 cases where you must use strict comparison

 

4 CASES WHERE YOU NEED TO USE STRICT COMPARISON OPERATORS

 

 

PHP variables’ type can change depending on context.

When you compare two variables or two values against each other, PHP automatically converts one of them (or sometimes both) so that they are of the same type before comparing them.

 

This happens when you use the standard, loose comparison operators, as in “$a == $b”.

This is why you can, for example, directly compare numeric strings against numbers even if they are of different types.

If you use strict comparison, however, no automatic conversion is done, and two values are never considered equal if their types differ.

 

Even if you normally prefer to use loose comparison operators, there are cases where you always need to use strict comparison.

Here are 4 common cases  where standard comparison does not work:

 

  1. Strings indexes

Functions that return substring positions, like strpos(), return FALSE when the substring is not found. But they can also return 0 (zero) when the substring is right at the start of the string, and standard comparison operators consider FALSE and 0 as equal:

<?php

$string = "It's bigger on the inside";
$substring = 'It';
$strpos = strpos($string, $substring);

if (!$strpos)
{
	echo 'Substring not found<br>';
}
else
{
	echo 'Substring found at position: ' . $strpos . '<br>';
}

echo 'Now with strict comparison...<br>';

if ($strpos === FALSE)
{
	echo 'Substring not found<br>';
}
else
{
	echo 'Substring found at position: ' . $strpos . '<br>';
}

 

  1. Array iteration

In most cases you probably want to use a foreach loop for array iteration, but in some cases you may want to iterate with next() (for example when you want to stop the iteration at any time and you prefer not to use break).

The next() function returns the next array value (using its internal pointer as reference) and returns FALSE when there are no values left.

When checking the next()‘s return value you need to use strict comparison, or if one array value is converted to boolean FALSE it may give a false positive:

 

<?php

$array = array(1, 5, 9, 0, 1, 4, 12, 8);

/* Print all array values */

do
{
	echo current($array) . '<br>';
}
while (next($array));

reset($array);
echo '<br>Using strict comparison:<br>';

do
{
	echo current($array) . '<br>';
}
while (next($array) !== FALSE);

 

  1. Numeric strings comparison

When you compare numeric strings, PHP default behaviour is to convert both to float numbers and then compare the results.

Because of the float numbers rounding problems, different strings may result in equal float numbers depending on the architecture where the PHP interpreter runs.

Float numbers with very small differences can therefore be rounded and represented as the same number, and thus considered equal.

In these cases you can use strict comparison to avoid the automatic float casting:

 

<?php

/* You may need to add more zeroes depending on your machine */
$a = '1.000000000000000001';
$b = '1.000000000000000002';

echo 'Standard comparison:<br>';

if ($a == $b)
{
	echo 'Equal<br>';
}
else
{
	echo 'Not equal<br>';
}

echo '<br>Strict comparison:<br>';

if ($a === $b)
{
	echo 'Equal<br>';
}
else
{
	echo 'Not equal<br>';
}

 

  1. Functions’ return values

Many PHP core functions return FALSE when some error occurs.

Some of them, however, can also return numeric values including 0 (zero). In these cases, it’s important to differentiate between FALSE and 0, using strict comparison.

One example is sleep(), which returns 0 on success or FALSE on failure:

 

<?php

/* Wrong */
if (!sleep(1))
{
	echo 'Sleep failed!<br>';
}
else
{
	echo 'Sleep succeeded!<br>';
}

/* Correct */
if (sleep(1) === FALSE)
{
	echo 'Sleep failed!<br>';
}
else
{
	echo 'Sleep succeeded!<br>';
}

 

If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

 

Alex