PHP Keeps Getting Better
PHP 7.3, released in December 2018, continues the trajectory of meaningful improvements that began with PHP 7.0. While it may not be as revolutionary as the jump from PHP 5 to 7, it introduces several features that improve developer experience, code readability, and runtime performance. At StrikingWeb, we have already upgraded our development environments and are recommending PHP 7.3 for new projects. Here is what you need to know.
Flexible Heredoc and Nowdoc Syntax
One of the most welcome quality-of-life improvements in PHP 7.3 is the relaxation of heredoc and nowdoc closing marker requirements. Previously, the closing marker had to appear at the very beginning of a line with no indentation, which made heredoc strings look awkward inside indented code blocks.
In PHP 7.3, the closing marker can be indented to match the surrounding code, and the corresponding indentation is stripped from all lines in the string. This makes heredoc strings practical for use inside functions, methods, and control structures without breaking code formatting conventions.
// PHP 7.3 - closing marker can be indented
function getHtml() {
return <<<HTML
<div class="wrapper">
<h1>Hello World</h1>
</div>
HTML;
}
This may seem minor, but in codebases that use heredoc for HTML templates or SQL queries, it significantly improves readability.
Trailing Commas in Function Calls
PHP 7.3 allows trailing commas in function and method calls. This feature, already available for arrays since PHP 5.0, reduces noise in version control diffs. When you add a new argument to a multi-line function call, only the new line shows up in the diff rather than the previous last line needing a comma added.
// Now valid in PHP 7.3
$result = someFunction(
$firstArg,
$secondArg,
$thirdArg, // trailing comma is now allowed
);
This is particularly useful for functions with many arguments and for code that is frequently modified.
array_key_first() and array_key_last()
Getting the first or last key of an array was surprisingly cumbersome before PHP 7.3. Developers resorted to reset() combined with key(), or used array_keys() to get all keys and then accessed the first or last element. PHP 7.3 introduces two dedicated functions that solve this cleanly:
$data = ['a' => 1, 'b' => 2, 'c' => 3];
$firstKey = array_key_first($data); // 'a'
$lastKey = array_key_last($data); // 'c'
These functions do not modify the array's internal pointer, which was a common gotcha with the reset() approach. They are also more readable and self-documenting than the workarounds they replace.
JSON Errors Now Throw Exceptions
Before PHP 7.3, json_encode() and json_decode() returned false or null on failure, requiring developers to call json_last_error() separately to check for errors. This led to error handling that was easy to forget.
PHP 7.3 adds the JSON_THROW_ON_ERROR flag, which causes JSON functions to throw a JsonException on failure:
try {
$data = json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// Handle the error properly
log_error('JSON decode failed: ' . $e->getMessage());
}
This integrates JSON error handling into PHP's standard exception mechanism, making it consistent with how other errors are handled in modern PHP applications.
is_countable() Function
PHP 7.2 introduced a warning when passing uncountable values to count(). This caused issues in codebases that relied on count() returning 0 for non-array values. PHP 7.3 adds is_countable() to make it easy to check whether a value can be counted before passing it to count():
if (is_countable($items)) {
$total = count($items);
} else {
$total = 0;
}
list() Reference Assignment
PHP 7.3 allows reference assignment in list() expressions, which was previously unsupported. This is useful when you want to destructure an array and modify values by reference:
$array = [1, 2, 3];
list(&$a, &$b, &$c) = $array;
$a = 10;
// $array is now [10, 2, 3]
Performance Improvements
While the feature additions are valuable, the performance improvements in PHP 7.3 are equally significant. Internal benchmarks show approximately 5-10 percent improvement in execution speed compared to PHP 7.2, depending on the workload. These improvements come from optimizations in the Zend Engine, including faster hash table operations, improved garbage collection, and optimized string operations.
For WordPress sites, upgrading from PHP 7.2 to 7.3 typically reduces average response time by 5-8 percent with no code changes. For Laravel applications, the improvement is similar, with some complex operations seeing up to 15 percent faster execution.
Deprecations to Watch For
PHP 7.3 deprecates several functions and behaviors that will be removed in future versions. Be aware of these if you are maintaining older codebases:
- Case-insensitive constants: Using
define()with the third parameter set totruefor case-insensitive constants is deprecated - image2wbmp(): Deprecated in favor of
imagewbmp() - Undocumented mbstring aliases: Several aliases for mbstring functions are deprecated
- FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED: These filter flags are deprecated as FILTER_VALIDATE_URL always checks for scheme and host
Migration Tips
Upgrading to PHP 7.3 is straightforward for most projects running PHP 7.1 or 7.2. Here is our recommended migration process:
- Check compatibility: Run your test suite on PHP 7.3 in a staging environment. Most frameworks (Laravel 5.5+, WordPress 5.0+, Symfony 4+) already support PHP 7.3.
- Review deprecation notices: Enable error reporting for deprecation notices and fix any warnings. These are not errors yet, but fixing them now prevents issues when upgrading to PHP 8.0 later.
- Update dependencies: Run
composer updateand check that all packages support PHP 7.3. Most actively maintained packages already do. - Test thoroughly: Pay particular attention to JSON handling, string functions, and any code that uses
count()with potentially non-array values. - Deploy to production: Use your standard deployment process. PHP minor version upgrades rarely require application code changes.
PHP 7.3 is a solid release that makes everyday development more pleasant while delivering tangible performance gains. If you are still running PHP 7.0 or 7.1 (both of which have reached end-of-life), upgrading to 7.3 should be a priority for both security and performance reasons.