...

/

Discovering Other PHP 8 Usage Changes

Discovering Other PHP 8 Usage Changes

Learn about typecasting and adding comments principles introduced in PHP 8.

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 ...