Understanding PHP 8 string-to-numeric comparison improvements
Comparing two numeric values has never been an issue in PHP. A comparison between two strings is also not an issue. A problem arises in non-strict comparisons between strings and numeric data (hardcoded numbers, or variables containing data of the float
or int
type). In such cases, PHP will always convert the string to a numeric value if a non-strict comparison is executed.
The only time a string-to-numeric conversion is 100% successful is when the string only contains numbers (or numeric values such as plus, minus, or the decimal separator). In this section, you learn how to protect against inaccurate non-strict comparisons involving strings and numeric data. Mastering the concepts presented in this chapter is critical if you wish to produce code with consistent and predictable behavior.
Before we get into the details of string-to-numeric comparisons, we need to first gain an understanding of what is meant by a non...