Skip to main content

PHP Has Released Its Latest Version 7.4: What’s New In It?

latest PHP version

PHP 7.4 is the latest PHP version released on November 28th, 2019. This latest version of PHP is faster and reliable as compared to previous PHP versions.

All thanks to the PHP version updates which brought us new features, fixes, syntax additions, and new commands. The new PHP version tests revealed many features and changes for the first time.

As a best web development company in USA, we in this article will tell you all the changes in brief. Once you understand each change and updated features, you can use PHP 7.4 for your web application projects in a fresh mood.

Let's Start With Highlights In PHP 7.4

The following are the changes made available in the latest PHP version. We will elaborate all of them, but before that just take a look at them!

  •  Arrow functions and cleaner one-liner functions
  •  Preloading feature to improve your web performance
  •  Typed properties in the classes
  •  Spread operator in arrays
  •  Improved type variance
  •  Use of underscores in numeric values
  •  Null coalescing assignment operator
  •  better extension development in PHP
  •  And more, you can read about it here

All these are the new changes or updated changes which we can find in PHP 7.4. To know more in deep, let’s proceed below:

Arrow functions

arrow function

Arrow functions or "short closures", allow less verbose one-liner functions.

In older PHP versions:
array_map(function (User $user) {
    return $user->id;
}, $users)

In latest version PHP 7.4:
array_map(fn (User $user) => $user->id, $users)

Notes about ARROW functions:

  •  Arrow functions access the parent scope
  •  No need to use the keyword
  •  $this is available the same as the normal closures
  •  Arrow functions only contain one line. Also, it can be the return statement

Typed Properties

typed properties

Typed properties weren’t present in PHP for a long time, but now they are available in the new PHP version - PHP 7.4. With the typed properties, declaring type hints to class properties and variables is an easy task.

However, in the old versions of PHP, it wasn’t possible. And you had to create getter and setter methods for enforcing type contacts.

Also Read: Laravel PHP Framework: Top Benefits Need To Know

Also, declaring types of static properties is easy now which wasn’t allowed before PHP version 7.4. You can use the following declaration methods for class variables & properties.

Type hint class variables as follows:
class A
    public string $name;
    public?Foo $foo;

Covariant Returns and Contravariant Parameters

PHP has the most invariant return types and parameters. Also, the subtype and supertype should have the same constraint for the parameter and return type.

To determine the congeniality of any method, the engine permits less specific parameter types, and more specific return types with the parents as long as the new types accept the parents specified types.

Or better say that a parameter type is substituted for its supertypes, while a return type substitutes a subtype. This is how you use covariant types in the latest PHP version PHP 7.4.

Use this code in the latest PHP version:

class A {}
        class B extends A {}
        class Producer {
            public function method(): A {}
        class ChildProducer extends Producer {
            public function method(): B {}



php preloading

Configure opcache in PHP 7.4 version for fast performance. In PHP 7.4, the changes are with the renewed concept. Basically, opcache will first compile the code files, then to avoid compilation’s repetition, it saves the compile files in shared memory.

To carry out preloading, specify opcache.preload in PHP.ini file. In addition to it, don’t forget to give the name or path of the PHP file.

Once the preloading of the file is done, using the file anytime during your project is easy. This is how uploading files at startup is easy which is made available on requests.

Therefore, preloading can achieve a significant performance boost for the overall web application.

The Assignment Operator: Null Coalescing

Null coalescing assignment operator is a shorthand of operations that wasn’t available in the older PHP versions 7.4.
Instead of doing this in new PHP 7.4:
$data['date'] = $data['date'] ?? new DateTime();
Do this in PHP 7.4:
$data['date'] ??= new DateTime();

Array spread operator

Next up, it's simply possible to use this array spread operator:
$arrayA = [1, 2, 3];
$arrayB = [4, 5];
$result = [0, ...$arrayA, ...$arrayB, 6 ,7];

// [0, 1, 2, 3, 4, 5, 6, 7]
This code only works with arrays having numerical keys.

Numeric Literal Separator

PHP 7.4 allows underscores to visually separate numbers or numeric values from each other. It looks like this:
$unformattedNumber = 107925284.88;
$formattedNumber = 107_925_284.88;

The engine in PHP simply ignores the underscores

Foreign Function Interface - FFI

It is a core-level feature where FFI allows the user to call C code from userland. As a result, PHP extensions are easily written in pure PHP and are loaded via a computer.

Though it looks a complex topic, you still need C language to properly use the FFI feature.

Also Read: Best Hybrid App Development Frameworks To Create Mobile Apps

Custom object serialization

PHP developers have added two new __serialize and __unserialize to give a proper serail order to codes, lines, files.

Reflection For References

Libraries like Symfony rely heavily on the API reflection to dump a variable reliably. Previously, this wasn’t possible in the older versions of PHP. But, now with PHP version 7.4 it is possible.

PHP 7.4 actually adds the ReflectionReference class which this variable issue.

Weak References

Weak references are references to those objects, which can't prevent themselves from being destroyed. The below function is able to provide the same functionality on the multi-byte strings.
mb_str_split added

Password Hashing Registry

There have been made some internal changes to the hashing libraries. More specifically, there is a new function, i.e. password_algos that actually returns a list of all the registered password algorithms. It looks small change but adds argon2i and argon2id hashing support whenever PHP is compiled without libargon.

Deprecations in PHP 7.4

The new features in PHP always come with the latest depreciation of the old features or components. With the release of PHP 7.4 let’s see some known deprecations:

Short Open Tags

Short open tags ?> are now deprecated and can be removed entirely in PHP 8.0.

Left-Associative Ternary Operator

In other languages, ternary operators are right-associative, but in PHP the ternary operator is left-associative. The latest version of PHP, 7.4 have removed the left-associativity in ternary operators but now requires parentheses.

For example:
1 ? 2 : 3 ? 4 : 5; // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok
1 ? 2 : (3 ? 4 : 5); // ok

Deprecate Curly Brace Syntax

In older versions of PHP, braces, and squares brackets were used to access array elements. But in PHP 7.4, you cannot use the curly braces because it will show parse error.
$array[] = 3;
echo $array[2]; // prints 3
$array{} = 3; // Parse error: syntax error, unexpected '}'

Several small deprecations

Here PHP has a list of few deprecated things. These are as follows:

  •  The real type
  •  Magic quotes legacy
  •  Array_key_exists(): checks whether a specified key or index is present within the array or not.
  •  Reflection export() methods
  •  Mb_strrpos() with encoding as 3rd argument: finds out the last occurrence of a string with another.
  •  Implode() parameter order mix: It returns the string from the elements of an array.
  •  Unbinding $this from non-static closures
  •  Hebrevc() function: Converts right-to-left flow of Hebrew text to a left-to-right flow.
  •  Convert_cyr_string() function: Converts the string from one Cyrillic character set to another.
  •  Money_format() function: It returns a formatted currency string.
  •  Ezmlm_hash() function: calculates the hash value upon need.
  •  Restore_include_path() function: Restores the value of the include_path configuration option.
  •  Allow_url_include ini directive: Allows data retrieval from remote locations if the function is enabled

So, all these changes and updates in PHP version 7.3 have moved us to the latest PHP 7.4. This new version looks more powerful and can easily complete all the development tasks.

All the dedicated developers in the top custom software development services in USA are in love with the new PHP version update. If you have some more queries on PHP 7.4, then don’t forget to ask it in the comment section below.

Submitted by cmsadmin on Mon, 12/02/2019 - 09:24

Awards and Accreditation