php version guide

PHP 7.0 Changes

PHP 7.0 is the starting point for many modernisation projects. It introduced scalar parameter declarations, return types, the null-coalescing operator, the spaceship operator, anonymous classes, and the Throwable hierarchy.

Changes Worth Recognising

  • Scalar declarations and return types make function contracts visible.
  • $value ?? $fallback replaces common isset() ternaries.
  • <=> provides a compact comparator for sorting.
  • Errors and exceptions both implement Throwable, which matters in top-level error handling.

Representative Code

PHP example
<?php

function displayName(?string $name): string
{
    return $name ?? 'Guest';
}

echo displayName(null) . PHP_EOL;
echo [3, 1, 2] <=> [3, 1, 4] ? "different\n" : "same\n";

// Prints:
// Guest
// different

Upgrade Review

  • Check old error handlers that only catch Exception.
  • Review newly added types against real inputs before enabling strict typing broadly.
  • Run the test suite under the target runtime; engine changes can reveal assumptions that syntax review misses.

Modern PHP code routinely relies on PHP 7.0 features, but legacy applications may still carry compatibility patterns written for PHP 5.

Practice

Review a PHP 5-Style Fallback
Show solution

Use $name = $input["name"] ?? "Guest";. The fallback is used both when the key is absent and when its value is null.