Home » Releases » PHP 8.0 »

PHP 8.0 Code Comparisons

Explore PHP 8.0

Named Arguments

PHP 8.0 introduces named arguments, allowing you to pass arguments to a function based on parameter names. This makes argument lists self-documenting, order-independent, and allows skipping optional parameters.
Before PHP 8.0
htmlspecialchars(
    $string,
    ENT_QUOTES | ENT_SUBSTITUTE,
    'UTF-8',
    true
);
PHP 8.0 or Later
htmlspecialchars(
    $string,
    double_encode: false,
    encoding: 'UTF-8',
);

Attributes

PHP 8.0 introduces native attributes (commonly known as annotations in other languages), allowing structured metadata to be added to declarations using native syntax rather than parsing PHPDoc docblocks.
Before PHP 8.0
class User
{
    /**
     * @Route("/api/users", methods={"GET"})
     */
    public function index()
    {
        // ...
    }
}
PHP 8.0 or Later
class User
{
    #[Route('/api/users', methods: ['GET'])]
    public function index()
    {
        // ...
    }
}

Constructor Property Promotion

PHP 8.0 adds constructor property promotion, reducing boilerplate by allowing class properties to be declared, typed, and assigned directly inside constructor parameters.
Before PHP 8.0
class Point
{
    public float $x;
    public float $y;

    public function __construct(
        float $x = 0.0,
        float $y = 0.0
    ) {
        $this->x = $x;
        $this->y = $y;
    }
}
PHP 8.0 or Later
class Point
{
    public function __construct(
        public float $x = 0.0,
        public float $y = 0.0
    ) {
    }
}

Match Expression

PHP 8.0 introduces match expressions, which act like switch but with strict type comparisons (===), returning values, no fall-through behavior, and exhaustive arm checks.
Before PHP 8.0
$result = '';
switch ($status) {
    case 200:
    case 300:
        $result = 'OK';
        break;
    default:
        $result = 'Error';
        break;
}
PHP 8.0 or Later
$result = match ($status) {
    200, 300 => 'OK',
    default => 'Error',
};