Discovering Other PHP 8 Usage Changes
Explore the important usage changes in PHP 8, including removed typecasts like real and unset. Understand modifications in anonymous function generation from class methods and how comment handling now differs with the introduction of Attributes. This lesson helps you adapt your code to PHP 8's stricter syntax and updated language features.
We'll cover the following...
There are a number of program code usage changes that we need to be aware of in PHP 8. We’ll start with a look at two typecasts that are no longer allowed.
Removed typecasts
Developers often use forced typecasts in order to ensure the data type of a variable is appropriate for a particular usage. As an example, when processing an HTML form submission, for the sake of argument, let’s say one of the form elements represents a monetary amount. A quick and easy way to sanitize this data element is to typecast it to a float data type, as follows:
$amount = (float) $_POST['amount'];
However, rather than typecast to float, some developers prefer to use real or double. Interestingly, all three produce exactly the same result. In PHP 8, the typecast to real has been removed. If our code uses this typecast, a best practice is to change it to ...