PHP 7: twice as fast, with scalar types
| Introduction | |
| Roughly twice as fast | |
| Scalar and return types | |
| ?? and <=> | |
| Engine errors you can catch | |
| The rest of the 7.x line |
Introduction
PHP 7 (2015) is two upgrades at once: the PHPNG engine roughly doubles real-world speed at same hardware, and the type system grows up — scalar declarations, return types, and engine errors you can finally catch. Most PHP 5 code runs faster untouched; new code gets stricter.
Author: Arthur Isaev
Roughly twice as fast
PHPNG (the new Zend Engine) halves request time and memory on typical CMS and framework workloads — the upgrade paid for itself on the hosting bill alone. No code changes required to collect most of it.
Scalar and return types
Parameters and returns finally declare int, string, bool, float — wrong types throw TypeError instead of silently coercing somewhere downstream:
<?php
namespace Aredel\Demo;
function headline(string $title): string
{
return '<h1>' . $title . '</h1>';
}
$name = $_GET['name'] ?? 'guest';
echo headline($name);
?? and <=>
?? (null coalescing) collapses the isset() ternary into one operator; <=> (spaceship) returns -1, 0 or 1, so comparators stop hand-rolling sign logic:
<?php
namespace Aredel\Demo;
$byLength = function (string $a, string $b): int {
return strlen($a) <=> strlen($b);
};
Engine errors you can catch
Fatal engine errors (bad calls, type failures) become Error throwables catchable with try / catch — the guard pattern from require fallback works uniformly now. Plus: anonymous classes for one-off test doubles and handlers.
The rest of the 7.x line
7.1: nullable types, void returns, iterable.
7.2: Argon2 passwords, libsodium crypto.
7.3: flexible heredoc, trailing commas in calls.
7.4: arrow functions, typed properties, spread in arrays.
Next: PHP 8 — JIT,
attributes, match and named arguments.
Article author: Arthur Isaev