New Features in PHP 7.3: An In-Depth Guide with Code Examples

Home / Blog / New Features in PHP 7.3: An In-Depth Guide with Code Examples


`PHP 7.3`, released on December 6, 2018, introduced several quality-of-life improvements, new functions, and syntax enhancements aimed at making code cleaner, safer, and more efficient. Below, we explore the most impactful new features in `PHP 7.3`, complete with explanations and practical code examples.

Flexible Heredoc and Nowdoc Syntaxes

`heredoc` and `nowdoc` syntaxes are popular for defining multi-line strings in `PHP`. Prior to `PHP 7.3`, their rigid requirements often made code less readable, especially in nested contexts. `PHP 7.3` brings two major improvements:

  • Indented Closing Markers: The closing marker can now be indented, and the leading whitespace is stripped from each line, allowing the `heredoc`/`nowdoc` to be aligned with the surrounding code.
  • No Trailing Newline Required: The closing marker no longer needs to be followed by a new line, which makes it easier to use `heredoc`/`nowdoc` inside arrays and function calls.

Example:

class Foo {
    public $bar = <<<EOT
        This is a
        multi-line string.
        Indented with code.
    EOT;
}

Array Example:

$values = [
    <<<END
    a
    b
    c
    END, // No newline required after END
    'd e f'
];

These changes greatly improve readability and reduce the likelihood of parse errors due to indentation mismatches.

Trailing Commas in Function Calls

`PHP 7.3` allows trailing commas in function and method calls, making it easier to add, remove, or reorder arguments-especially in multi-line calls. This was previously only allowed in arrays.

Example:

myFunction(
    $arg1,
    $arg2,
    $arg3, // Trailing comma is now valid
);

This also applies to method and closure calls:

$foo->bar(
    'param1',
    'param2',
);

This small change aids in cleaner diffs and easier code maintenance.

New `is_countable()` Function

Counting a non-countable variable (like a `string` or `integer`) in `PHP 7.2` and earlier would trigger a `warning`. `PHP 7.3` introduces `is_countable()`, which checks if a variable is countable before using `count()`.

Example:

$a = "Hello";
$b = ["red", "green", "blue"];

echo is_countable($a) ? count($a) : "Not countable"; // Output: Not countable
echo is_countable($b) ? count($b) : "Not countable"; // Output: 3

This function returns `true` for arrays and objects implementing `Countable`, and `false` otherwise.

New `array_key_first()` and `array_key_last()` Functions

These helper functions quickly retrieve the `first` and `last keys` of an `array`, simplifying a common pattern and improving code clarity.

Example:

$array = [
    'first' => 'apple',
    'second' => 'banana',
    'third' => 'cherry'
];

echo array_key_first($array); // Output: first
echo array_key_last($array);  // Output: third

They work for both associative and indexed arrays, and return `null` for empty arrays.

`JSON_THROW_ON_ERROR` Option

`PHP 7.3` introduces the `JSON_THROW_ON_ERROR` flag for `json_encode()` and `json_decode()`, which throws a `JsonException` on error instead of setting a global error state. This makes error handling more robust and object-oriented.

Example:

try {
    $result = json_decode('invalid json', false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
    echo "JSON error: " . $e->getMessage();
}

This approach is safer and avoids silent failures or reliance on `json_last_error()`.

`list()` Reference Assignment

`PHP 7.3` allows reference assignments in `list()`, enabling direct modification of array elements during unpacking.

Example:

$array = [1, 2, 3];
list(&$a, $b, $c) = $array;
$a = 10;
print_r($array); // Output: [10, 2, 3]

This feature is particularly useful when you need to update array values after `destructuring`.

Argon2 Password Hash Enhancements

PHP 7.3 extends support for the `Argon2` password hashing algorithm by adding `Argon2id`, which combines the features of `Argon2i` and `Argon2d` for better security.

Example:

$password = 'secret';
$hash = password_hash($password, PASSWORD_ARGON2ID);

This offers developers more flexibility and security options for password storage.

Other Notable Improvements

  • Performance: `PHP 7.3` is up to `10% faster` than `PHP 7.2`, offering significant performance improvements for most applications.
  • `SameSite` Cookie Support: New options for setting the `SameSite` attribute on cookies, improving security for web applications.
  • Deprecations and Cleanups: Several legacy features and functions have been deprecated or removed, continuing `PHP`’s modernization efforts.

Conclusion

`PHP 7.3` focuses on developer productivity, code readability, and security. With features like flexible `heredoc`/`nowdoc` syntax, trailing commas in function calls, new utility functions, and improved error handling, `PHP` codebases can now be cleaner, safer, and easier to maintain. If you haven’t already, consider upgrading your projects to leverage these enhancements.


Written by X2Y.DEV
PHP 7.3 Web Dev

0%