skip to content

In PHP 8.5, which cast spellings are deprecated or removed, and which new warnings can an explicit cast now raise?

level: middleimportance: nice to knowfreq 16%

answer

  1. four non-canonical aliases
  2. (integer) (boolean) (double) (binary)
  3. (real) and (unset) gone since 8.0
  4. float not representable as int warns
  5. (void) is not a conversion

basics

~20 s

PHP 8.5 deprecates the casts (integer), (boolean), (double) and (binary); (real) and (unset) were removed in 8.0. Casting an out-of-range float to int, or NAN, now warns. The new (void) cast discards a value, converting nothing.

solid answer

~40 s

PHP has one canonical spelling per cast: `(int)`, `(float)`, `(string)`, `(bool)`, `(array)` and `(object)`. **PHP 8.5** deprecates the aliases `(integer)`, `(boolean)`, `(double)` and `(binary)`; each now emits `E_DEPRECATED`, for example "Non-canonical cast (integer) is deprecated, use the (int) cast instead". Two older aliases are already gone: `(real)` and `(unset)` were removed in **PHP 8.0** and now stop compilation. PHP 8.5 also adds **warnings** to casts that used to be silent: casting a float, or a float-looking string, that cannot be represented as an `int` warns "... is not representable as an int, cast occurred", and casting `NAN` to another type warns as well. Finally, 8.5 introduces `(void)`, which is not a conversion: it marks a discarded return value as intentional. The migration is mechanical: replace the aliases, which a code-style fixer can do automatically.

code

php · 13 lines
php
<?php
declare(strict_types=1);

$raw = '12';

$a = (integer) $raw;  // Deprecated: Non-canonical cast (integer) is deprecated, use the (int) cast instead
$b = (int) $raw;      // canonical, no diagnostic
$c = (double) $raw;   // Deprecated: ... use the (float) cast instead
$d = (float) $raw;

var_dump($a === $b, $c === $d); // bool(true) bool(true)

// $e = (unset) $raw;  // removed in PHP 8.0: does not compile

go deeper

for a junior

Know the canonical cast names, (int), (float), (string), (bool), (array) and (object), and that aliases like (integer) are deprecated.

for a middle

Separate the 8.5 deprecations, (integer), (boolean), (double) and (binary), from the 8.0 removals, (real) and (unset), and explain the new out-of-range warning.

for a senior

Plan the 8.5 upgrade step: automated alias replacement, running tests with deprecations visible, and reviewing casts of large floats that now warn.

for a principal

Treat deprecation cleanup as continuous maintenance, with automated fixers in CI so each PHP upgrade does not become a separate project.

## One spelling per cast A PHP **cast** converts a value by writing the target type in parentheses before it. The canonical casts are: - `(int)`, `(float)`, `(string)`, `(bool)`: the scalar casts; - `(array)` and `(object)`: the compound casts. Whitespace inside the parentheses is ignored, so `( int )` is the same as `(int)`. Over the years PHP also accepted alternative spellings borrowed from older type names, and PHP 8.0 and 8.5 have been removing them. ## Deprecated in PHP 8.5 PHP 8.5 deprecates the four remaining **non-canonical** aliases. Each still works, but emits an `E_DEPRECATED` at compile time with a message naming the replacement: | Deprecated alias | Use instead | Message in 8.5 | |---|---|---| | `(integer)` | `(int)` | Non-canonical cast (integer) is deprecated, use the (int) cast instead | | `(boolean)` | `(bool)` | Non-canonical cast (boolean) is deprecated, use the (bool) cast instead | | `(double)` | `(float)` | Non-canonical cast (double) is deprecated, use the (float) cast instead | | `(binary)` | `(string)` | Non-canonical cast (binary) is deprecated, use the (string) cast instead | `(binary)` existed as "forward support" for a binary string type that never arrived; it has always been identical to `(string)`. ## Removed in PHP 8.0 Two aliases are already gone: - **`(real)`**, an alias of `(float)`, was deprecated in 7.4 and removed in 8.0. - **`(unset)`**, which cast to `null`, was deprecated in 7.2 and removed in 8.0. Assigning `null` or calling `unset()` is the replacement. Code that still contains either one does not compile on PHP 8: `(real)` raises a `ParseError`, "The (real) cast has been removed, use (float) instead", and `(unset)` a compile error, "The (unset) cast is no longer supported". ## New warnings on casts in PHP 8.5 Casts used to be completely silent. PHP 8.5 adds two warnings: 1. **Out-of-range floats to int.** When a float, or a string holding a float such as `'1e30'`, cannot be represented as an `int`, the cast still happens but PHP warns: "The float ... is not representable as an int, cast occurred" or "The float-string ... is not representable as an int, cast occurred". This applies to explicit `(int)` casts and to implicit conversions to int. What the resulting integer is, and why it overflows, belongs with integer and float limits; the point here is that the cast is no longer silent. 2. **`NAN` to other types.** Casting `NAN` to another type now emits a warning. String-to-int casts of ordinary leading-numeric or non-numeric strings are still silent: `(int) '42abc'` is `42` and `(int) 'abc'` is `0`, with no diagnostic. ## (void): a cast that converts nothing PHP 8.5 also adds `(void)`. The manual is explicit that it is **not a value conversion**: it is used as a statement to discard the result of an expression intentionally, mainly to acknowledge a function marked `#[\NoDiscard]` without triggering its warning. It does not produce a value you could assign. ## Migrating a codebase - Search for the four aliases and replace them; code-style fixers ship a rule that normalises cast spelling automatically. - Run the test suite on 8.5 with deprecations visible; each alias reports its own line. - Review any `(int)` cast on data that might be a very large float or a scientific-notation string, since those now warn. - In `settype()`, the type names are strings rather than cast syntax; `settype()` still accepts both `'int'` and `'integer'`, `'bool'` and `'boolean'`, `'float'` and `'double'`. ## Why interviewers ask This is a "have you kept up" question. A strong answer lists the four deprecated aliases without mixing them up with the two removed ones, knows the new out-of-range warning, and does not confuse `(void)` with a conversion.

  • Does (integer) $x still work in PHP 8.5?
    Yes, it still converts to int exactly like `(int)`, but it now emits `E_DEPRECATED` "Non-canonical cast (integer) is deprecated, use the (int) cast instead". A deprecation means it is expected to be removed in a future major version, so replacing it now is cheap insurance.
  • What replaced the (unset) cast after PHP 8.0 removed it?
    Nothing with cast syntax. `(unset) $x` only ever evaluated to `null`, so the replacement is to use `null` directly, `$y = null;`, or `unset($y)` when the goal is to remove a variable. Code still containing `(unset)` fails to compile on PHP 8.
  • Is (void) a way to convert a value to null in PHP 8.5?
    No. `(void)` does not produce a value at all; the manual says it is not a value conversion. It is used as a statement to discard an expression's result on purpose, mainly to silence the warning from a function marked `#[\NoDiscard]`.

saying these in an interview costs you the question

  • Says (real) is deprecated in 8.5 rather than removed in 8.0.
  • Believes (integer) stopped working entirely in PHP 8.5.
  • Thinks (void) converts a value to null.
  • Claims casts in PHP 8.5 still never emit any diagnostic.
  • Says (binary) produces a different type from (string).