PHP 7.3 was officially released in December 2018. Not a revolution, but a few changes have a real impact on everyday code – especially working with JSON, more flexible Heredoc syntax, and a handful of new array functions.
json_decode() with exceptions instead of silent failures
Before PHP 7.3, malformed JSON simply returned null – no exception, no error. You always had to check json_last_error(). PHP 7.3 adds JSON_THROW_ON_ERROR:
// PHP 7.3 - exception on parse error
try {
$data = json_decode('{"broken: json}', true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
echo $e->getMessage(); // Syntax error
}
// Works with json_encode() too
try {
$encoded = json_encode(['key' => "±1"], JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
echo $e->getMessage(); // Malformed UTF-8 characters
}
Flexible Heredoc and Nowdoc
Before PHP 7.3 the closing marker had to be at column zero. PHP 7.3 allows indentation:
// PHP 7.3 - closing marker can be indented
function buildQuery(): string
{
return <<<SQL
SELECT *
FROM orders
WHERE status = 'pending'
SQL;
}
array_key_first() and array_key_last()
$data = ['apple' => 1, 'banana' => 2, 'cherry' => 3]; // PHP 7.3 - direct $firstKey = array_key_first($data); // 'apple' $lastKey = array_key_last($data); // 'cherry'
is_countable() – end of warnings on PHP 7.2
function countItems($items): int
{
if (!is_countable($items)) {
return 0;
}
return count($items);
}
Summary
JSON_THROW_ON_ERROR is a change worth adopting immediately – it eliminates an entire class of silent failures when working with APIs. Flexible Heredoc is useful for long SQL queries or XML templates in Magento modules.
