What's New in PHP 7.3 in 30 Seconds in Diffs

No time but eager to hear PHP news? PHP 7.3 is out in December 2018 and it brings 173 changes. Which are the most useful ones?

From features that might be the most interesting to those lesser ones, that anybody will rarely use.

1. Comma After the Last Argument

  See RFC

Do you know this?

 $array = [
     1,
+    2,
 ];

We'll be able to do this:

 $this->someFunction(
     $arg,
+    $arg2,
 );

But still not this:

 function someFunction(
     $arg,
+    $arg2,
 ) {}

Thanks Jacob for pointing this difference out.


25 seconds to go...


2. First and Last Array Key

  See RFC

 $items = [
     1 => 'a',
     2 => 'b',
 ];

-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
 var_dump($firstKey); // 1
 $items = [
     1 => 'a',
     2 => 'b',
 ];

-end($items);
-$lastKey = key($items);
+$lastKey = array_key_last($items);
 var_dump($lastKey); // 2

These will be handy in coding standard tools.


Still 15 seconds...


3. Countable for Risky Variables

  See RFC

I don't think having a variable of 2 forms is a good idea:

<?php

$items = null; // same as "private $items;" in a class

echo sprintf('There is %d items', count($items));
// error Warning: count(): Parameter must be an array or an object that implements Countable

But in case of that smelly (3rd party) code, there is a help:

 $items = null;

+if (is_countable($items)) {
-echo sprintf('There is %d items', count($items));
+     echo sprintf('There is %d items', count($items));
+}

Only 5 seconds, hurry!


4. Safer JSON Parsing

  See RFC

-json_encode($data);
+json_encode($data, JSON_THROW_ON_ERROR);
-json_decode($json);
+json_decode($json, false, 512, JSON_THROW_ON_ERROR);

So you'll be able to do:

try {
    return json_decode($json, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
    // ...
}

I've used similar technique for years thanks to Nette\Utils and I've never complained:

<?php

try {
    return Nette\Utils\Json::encode($value);
} catch (Nette\Utils\JsonException $exception) {
    // ...
}

...0, you made it! Congrats, now get back to pwning the world!


Did I miss a feature you plan to use from day 1 of your PHP 7.3? I might too, drop it in the comments!


  Travis Knows the Code Works

The code used in this post is tested daily with Travis CI. You can see tests on Github.

Thanks to tests this post:

  • always run against the most recent dependencies
  • gets updates and stays relevant for many years even when new major version of PHP or Symfony is released

Found a typo? Fix it to join team of 73 people that improve content here

❤️️ Do you find value in topics what I write about?
Support my writing by throwing a couple bucks at my Patreon