PHP 7.0 launched in December 2015, and by mid-2017 PHP 7.1 has solidified the upgrade story. The headline number: PHP 7 is roughly twice as fast as PHP 5.6 on real-world WordPress and Drupal benchmarks, and it consumes significantly less memory. That alone justifies upgrading. But the language features are equally compelling.
Scalar Type Declarations
PHP 7 lets you type-hint scalar parameters: int, float, string, bool. In strict mode (declare strict_types=1 at the top of each file), PHP throws a TypeError if the wrong type is passed:
<?php
declare(strict_types=1);
function multiply(int $a, int $b): int {
return $a * $b;
}
echo multiply(4, 5); // 20
echo multiply('4', 5); // TypeError in strict mode
Return Type Declarations
Functions can now declare what type they return. Combined with scalar type hints, your function signatures become self-documenting contracts that the engine enforces.
The Null Coalescing Operator (??)
Replace verbose isset() chains with a concise ??:
// Before PHP 7
$username = isset($_POST['user']) ? $_POST['user'] : 'guest';
// PHP 7
$username = $_POST['user'] ?? 'guest';
The Spaceship Operator (<=>)
Returns -1, 0, or 1 — perfect for usort() callbacks:
usort($people, function($a, $b) {
return $a['age'] <=> $b['age']; // sort ascending by age
});
Anonymous Classes
Inline class definitions for one-time use, test mocks, or lightweight implementations of interfaces:
$obj = new class(10) {
public function __construct(private int $value) {}
public function double(): int { return $this->value * 2; }
};
echo $obj->double(); // 20
PHP 7.1 Additions
- Nullable types —
?stringaccepts a string or null - Void return type — explicitly declares a function returns nothing
- Class constant visibility —
public/protected/private const - list() in foreach — destructure nested arrays cleanly
Upgrading to PHP 7 is one of the highest-ROI technical decisions you can make in 2017. If your host doesn’t offer PHP 7, find a new host.