'Why are the results different when using <=> operator in PHP 7 and PHP 8?
Why are the results different when using <=> operator in PHP 7 and PHP 8?
In PHP 7:
echo 76 <=> '76 trombones'; // displays 0
In PHP 8:
echo 76 <=> '76 trombones'; // displays -1
Solution 1:[1]
PHP 8 changed the way non-strict comparison between numbers and non-numeric strings work.
<=> is a non-strict comparison.
The values in your example, 76 and '76 trombones' evaluated as equal in PHP 7 because the string was cast to a number for the comparison, and (int) '76 trombones' is 76.
Now in PHP 8, the number is cast to string for the comparison instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
