Differences in Arithmetic, Bitwise, and Concatenation Operations
Explore the significant changes PHP 8 introduces in arithmetic, bitwise, and concatenation operations. Understand how non-scalar data types are now restricted in arithmetic, and how the concatenate operator's precedence has shifted below arithmetic and bitwise operators. This lesson helps you identify potential migration issues and avoid hidden errors in PHP 8.
We'll cover the following...
Arithmetic, bitwise, and concatenation operations are at the heart of any PHP application. In this lesson, we’ll learn about hidden dangers that might arise in these simple operations following a PHP 8 migration. We’ll learn about these changes made in PHP 8 so that we can avoid a potential code break in the application. Because these operations are so ordinary, without this knowledge, we will be hard-pressed to discover post-migration errors.
Let’s first have a look at how PHP handles non-scalar data types in arithmetic and bitwise operations.
Handling non-scalar data types in arithmetic and bitwise operations
Historically, the PHP engine has been very forgiving about using mixed data types in an arithmetic or bitwise operation. We’ve already had a look at comparison operations that involve numeric, leading-numeric, and non-numeric strings and numbers. As we learned, when a non-strict comparison is used, PHP invokes type juggling to convert the string to a number before performing the comparison. A similar action takes place when PHP performs an arithmetic operation that involves numbers and strings.
Prior to PHP 8, non-scalar data types (data types other than string, int, float, or boolean) were allowed in an arithmetic operation. PHP 8 has clamped down on this bad ...