Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Concepts and Reference

Deprecated and removed PHP features

A practical list of PHP features that are deprecated or removed in PHP 8.x, how deprecation notices work, how to find them in your code and what to use instead.

PHP removes features slowly. First a feature is deprecated: it still works, but PHP raises an E_DEPRECATED notice that says it will go away. Later, a major or minor release removes it, and code that still uses it fails with an error. Reading the deprecation notices on your test server is the cheapest way to prepare for an upgrade.

This page lists the changes that catch the most code, and shows how to make PHP tell you about them.

PHP
<?php
error_reporting(E_ALL);             // include E_DEPRECATED
set_error_handler(function (int $level, string $message) {
    echo "[$level] $message\n";
    return true;
});

Try it

PHP Catching deprecation notices with an error handler

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Fixing the same code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Notable changes by version

Version Change Use instead
8.0 Removed: create_function(), each(), curly-brace string offsets $s{0} Closures, foreach, $s[0]
8.0 Optional parameter before a required one is deprecated Reorder, or make the later parameter required
8.1 Passing null to non-nullable built-in parameters is deprecated Cast or default first ($x ?? "")
8.1 strftime(), gmstrftime(), date_sunrise() deprecated date(), DateTimeImmutable::format(), IntlDateFormatter
8.1 FILTER_SANITIZE_STRING deprecated htmlspecialchars() or proper validation
8.2 Dynamic properties deprecated Declare properties, or #[AllowDynamicProperties]
8.2 ${var} and ${expr} string interpolation deprecated {$var} or $var
8.2 utf8_encode() and utf8_decode() deprecated mb_convert_encoding()
8.3 get_class() and get_parent_class() without arguments deprecated self::class, parent::class
8.4 Implicitly nullable parameter types deprecated ?Type $x = null
8.4 Underscore as a class name, E_STRICT constant Another name, E_ALL
8.5 Backtick shell operator, (boolean), (integer), (double), (binary) casts, null as an array offset shell_exec(), (bool), (int), (float), (string)

The table lists what was announced for each version at the time of writing. Always confirm against the official migration guide for the exact version you target, since some deprecations are scheduled for removal several releases later.

Finding deprecated code

  • Run your tests and the application with error_reporting = E_ALL and display_errors on in development, and log in production.
  • Static analysis (PHPStan, Psalm) and Rector rulesets for each PHP level report and often rewrite deprecated usage.
  • php -l file.php catches only syntax errors, not deprecated runtime calls, but it does flag compile-time deprecations.
  • Composer reports packages that do not support your PHP version when you run composer update.

Usage notes

  • A deprecation notice is a warning about the future, not a bug today. Do not silence it with @; fix it.
  • Some deprecations are raised at compile time, before your error handler is installed. Put set_error_handler() in an auto-prepended file or test them through eval(), as above.
  • Libraries often support a wide PHP range. Check your dependencies before upgrading PHP itself.
  • Removed features throw Error (for example Call to undefined function each()), not a notice.