PHP

What’s new in PHP 7.3?

PHP 7.3

Since the release of PHP 7 , there have been a lot of new features that have greatly improved the way developers can use PHP in their projects. The main objective of these new functions is to improve the performance and security of PHP applications.

The new version of PHP 7.3 comes with some very necessary updates.

In this post, we will explain some of the functions and updates of PHP 7.3 recently introduced. Remember that from the management panel of your accommodation in DonDominio you can change the PHP version at any time.

PHP 7.3 improvements

  • Flexible Heredoc and Nowdoc syntax
  • Allow a final comma in function calls
  • JSON_THROW_ON_ERROR
  • PCRE2 migration
  • list () Reference assignment
  • Is_countable function
  • array_key_first (), array_key_last ()
  • Hash Argon2 Password Enhancements
  • Deprecated and deleted image2wbmp ()
  • Deprecated and eliminated Case-Insensitive Constants
  • FPM updates

Flexible Heredoc and Nowdoc syntax

The Heredoc and Nowdoc syntax help users to use multi-line long strings. It requires that the final identifier be the first string that appears in a new line.

Basically this update proposes two new changes that are the following:

  • To enable the closing marker
  • To eliminate the new line requirement after the closing marker

In the example, mentioned above, you can see the two changes easily.

Allow a final comma in function calls

The final comma addition is made at the end of the enumerated parameters, elements and variables. Several times we call a series of elements in an array or function (especially variadic functions), in which if a comma has been omitted, they will generate an error. In order to avoid that problem, the final comma can be useful. Since PHP 7.2 its use in syntax of grouped names is allowed.

use Foo\Bar\{
   Foo,
   Bar,
};

$foo = [
   'foo',
   'bar',
];

As new values ​​are added, the final commas are very useful.

In varied functions like unset (), they will do the job properly:

unset(
   $foo,
   $bar,
   $baz,
);

When you send a list of variables through the compact () function to a template engine, you can also use:

echo $twig->render(
   'index.html',
   compact(
       'title',
       'body',
       'comments',
   )
);

It is often necessary to combine arrays with the array_merge () function in situations where concatenation or data pooling is built. You can use the ending comma there:

$newArray = array_merge(
   $arrayOne,
   $arrayTwo,
   ['foo', 'bar'],
);

Similarly, you can use it in methods, function arguments, and closing calls.

class Foo
{
 public function __construct(...$args) {
   //
 }

 public function bar(...$args) {
   //
 }

 public function __invoke(...$args) {
   //
 }
}

$foo = new Foo(
 'constructor',
 'bar',
);

$foo->bar(
 'method',
 'bar',
);

$foo(
 'invoke',
 'bar',
);

JSON_THROW_ON_ERROR

To parse the JSON responses, we have two available functions json_encode () and json_decode (). Unfortunately, Json_encode () will only throw a false error, while json_decode will throw a null value, and that value can be true. The only way to know the error is to call json_last_error () or json_last_error_msg (), which return the global error status in readable forms for both machine and human.

In this RFC, the proposed solution is to add JSON_THROW_ON_ERROR in both functions, which ignores the global error status. If an error occurs, these functions throw a JsonException with the message and the code set to any json_last_error () and json_last_error_msg (). You can use it as:

json_encode($data, JSON_THROW_ON_ERROR);

json_decode("invalid json", null, 512, JSON_THROW_ON_ERROR);

PCRE2 migration

PHP uses PCRE for regular expressions. From PHP 7.3, PCRE2 will come into action. Therefore, we must migrate existing regular expressions according to the rules of PCRE2. These rules will be more aggressive than before. For example, the regular expression:

preg_match('/[\w-.]+/', '');

This regular expression will now fail and will not generate a warning error. Because PCRE2 is strict with respect to moving the script to the end.

These and more features are available with the update to PCRE2 10.x:

  • Forward the relative inverse references, \ g {+2} (reflecting the existing \ g {-2})
  • Version verification available through patterns such as (? (VERSION> = x) …)
  • (* NOTEMPTY) and (* NOTEMPTY_ATSTART) tell the engine not to return empty matches)
  • (* NO_JIT) disables JIT optimization
  • (* LIMIT_HEAP = d) sets the heap size limit in kilobytes d
  • (* LIMIT_DEPTH = d) sets the tracking limit in d
  • (* LIMIT_MATCH = d) sets the match limit in d

list () Reference assignment

PHP has list () assignment and reference functions. But in the current state you can not use the reference assignment in the list () function. From now on, with PHP 7.3 its use will be allowed. The new improved syntax is the following:

$array = [1, 2];
list($a, &$b) = $array;

//Equivalente a 

$array = [1, 2];
$a = $array[0];
$b =& $array[1];

The new changes in PHP 7.3 will also allow us to use this with the nested lists and the foreach () function:

$array = [[1, 2], [3, 4]];
foreach ($array as list(&$a, $b)) {
   $a = 7;
}
var_dump($array);

Is_countable function

In PHP 7.2, you can count objects and matrices through the count () function. If the object is not countable, PHP issues a warning. It is necessary to check whether the object or variable is countable or not. While PHP 7.3 now has a new is_countable () function that returns if the passed variable is countable or not.

This RFC proposes a new type function that returns true if the given value is a type of matrix or an instance of the accounting interface.

Antes:

if (is_array($foo) || $foo instanceof Countable) {
   // $foo is countable
}

Después:

if (is_countable($foo)) {
   // $foo is countable
}

array_key_first (), array_key_last ()

Currently, PHP allows you to retrieve the first and last key / value of the array by changing the internal state of the array when the reset (), end (), and key () methods are used. Now to avoid this disorder in the internal state, PHP 7.3 proposes new functions and adds them to the kernel:

  • $ key = array_key_first ($ array); To gather the first key of the matrix
  • $ key = array_key_last ($ array); To gather the last key of the matrix
  • $ value = array_value_first ($ array); To gather the first value of the matrix
  • $ value = array_value_last ($ array); To gather the last value of the matrix

Let’s see the example:


// Uso de un array asociativo

$array = ['a' => 1, 'b' => 2, 'c' => 3];
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
assert($firstKey === 'a');
assert($lastKey === 'c');

// Uso de un array numerico

$array = [1 => 'a', 2 => 'b', 3 => 'c'];
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);
assert($firstKey === 1);
assert($lastKey === 3);

Hash Argon2 Password Enhancements

In previous versions of PHP, we obtained the hashing of Argon2 passwords, which is a modern algorithm for securing passwords using hashes. It comes in three different types, Argon2i, Argon2d and Argon 2id. Argon2i is optimized for password hashing and password-based key derivation. Argon2d is faster and uses access to memory based on the data. Argon2i instead uses access to independent data memory. Argon2id is a hybrid of Argon2i and Argon2d, which uses a combination of memory-dependent and memory-independent access to the data.

password_hash ():

Argon2id is now the recommended variant of Argon2 to use in the paswword_ * functions.

// Argon2id por nombre con factores de coste personalizados se comporta igual que PASSWORD_ARGON2I

password_hash('password', PASSWORD_ARGON2ID, ['memory_cost' => 1<<17, 'time_cost' => 4, 'threads' => 2]);

password_verify ();

The password_verify () function works with Argon2id in addition to Argon2i.

password_needs_rehash ();

This function will also accept the Argon2id hash and if the cost factors change, this will become true.

$hash = password_hash('password', PASSWORD_ARGON2ID);
password_needs_rehash($hash, PASSWORD_ARGON2ID); // false
password_needs_rehash($hash, PASSWORD_ARGON2ID, ['memory_cost' => 1<<17]); // true

Deprecated and deleted image2wbmp ()

This function returns the bitmap or WBMP image format. Another imagewbmp () function is also available doing the same thing that is supposed to handle the necessary monochromatic conversion. Therefore, due to duplication, image2wbmp () is now deprecated and in PHP7.3 it can be replaced with imagewbmp (). After deprecation, every call to image2wbmp () will issue a warning disapproval. After its removal, every call to image2wbmp () would generate a fatal error.

Deprecated and eliminated Case-Insensitive Constants

At this time, you can use constants that distinguish between uppercase and lowercase and others that do not. But constants insensitive to uppercase and lowercase created complexities of bits in use. Therefore, to address this, PHP 7.3 proposes to discard constants that do not distinguish between uppercase and lowercase.

Currently, the situation is:

  • Class constants always distinguish between uppercase and lowercase.
  • The global constants declared with const are always case-sensitive.
  • The constants declared with define () are case-sensitive by default.
  • It is possible to declare constants that do not distinguish between uppercase and lowercase when passing true as the third parameter of define ().

PHP 7.3 proposes the following changes to solve this:

  • Deprecate the call to define () with the third parameter as true.
  • Deprecate access to a constant that is not case-sensitive with a different cover than the declaration site. The constants true, false and null are exempt from this.

Updates to FPM

The FastCGI process manager also has an update and now comes with new options to customize the FPM registry.

log_limit : Set the allowed length for log messages. This limit could be more than 1024 characters without adjustment.

log_buffering : Allows experimental registration without additional buffer.

Final note

In this post we have discussed some of the most important topics of PHP 7.3, if you want to see all the information about the updates you can access the php.net page .

Leave a Comment