Skip to content

38: Replace Deprecated Numeric Aliases

Migrate type checks and conversions to is_float(), is_int(), and floatval(). The aliases is_double(), is_long(), is_integer(), and doubleval() are deprecated.

  • PHP 8.6 CLI. Confirm the version with php --version.
  • Review the previous chapter if you’re following the series in order.

Allow about 10 minutes for the walkthrough and exercise.

A runnable numeric-aliases.php example.

Run the following from the root of the Code with PHP repository.

Terminal window
php code/php-8-6/38-numeric-aliases/numeric-aliases.php
numeric-aliases.php
<?php
declare(strict_types=1);
foreach ([42, 4.25, '4.25'] as $reading) {
printf("%s int=%s float=%s cast=%.2f\n", get_debug_type($reading),
is_int($reading) ? 'yes' : 'no',
is_float($reading) ? 'yes' : 'no',
floatval($reading));
}
int int=yes float=no cast=42.00
float int=no float=yes cast=4.25
string int=no float=no cast=4.25

Each replacement preserves the corresponding alias’s behavior. A numeric string can be converted but still fails an integer or float type check. Keep validation separate when user input may be malformed.

Add the string '42' to the inputs, alongside the existing integer 42. Confirm that only the integer value passes is_int() and both can convert to a float.

Continue with Replace spl_object_hash() and spl_classes().