Main News of PHP 7.4

Ronaldo B.July 27, 20227 min read

In this article

PHP 7.4 was released in November 2019 and brought changes that made PHP even more robust. In this article, we will see the main new features of this version and analyze in which situations they can help us.

Data typing in attributes

PHP is known for being a loosely typed language, meaning it does not require us to define the type of a variable when we create it, nor does it prevent us from changing the type of that variable if we wish.

However, version 7.4 brought us a small novelty that alters this concept, at least in the scope of Object-Oriented Programming. Now we can define the type of attributes that we create in our classes. Let's see an example by creating a class called “Person”:

class Person {

    public int $id;

    public string $name;

}

With this code, we defined that the attribute $id must be of type integer and the attribute $name must be a string.

Let's create a new instance of this class and set a value for the attribute $id:

$glaucio = new Person();

$glaucio->id = "150";

We defined that its value will be the number 150. This code will work because it respects what we defined when creating the attribute.

However, what happens if we provide the value “null” for it? Let's instantiate a new object for that.

$joao = new Person();

$joao->id = null;

When testing the code in the browser, we see the following result:

Error returned in our browser

The message informs us that the attribute $id must be an integer and returned a fatal error, stopping the execution of the code, which shows that the typing is working.

But let's think of one more situation. What happens if we provide a string? Let's test it.

$joao->id = "150";

Now the attribute $id has the value of the string “150”. What happens in this case? The code will work! This occurs because PHP will attempt to convert the provided value to an integer and will only return an error if it cannot. In the case of a string, the conversion to an integer is possible.

If you want to make PHP as “strict” as possible regarding data types, just add this line at the beginning of the code:

declare (strict_types = 1);

Now, if we test the same code, we will see the following result in the browser:

Error in our browser

Now the check has been fully performed. Remembering that we teach everything about Object-Oriented Programming in PHP in our Complete PHP 7 Course.

How can this change be helpful? You may already be used to using PHP without data typing and may not see this change as a good thing. However, we can better face this change if we see it as an additional resource and identify some situations where it can be useful. For example: this novelty helps us reduce the chances of encountering type errors in our code, which can save us some headaches ?.

Arrow Functions

If you already work with JavaScript, I imagine you are used to using Arrow Functions in your code. In fact, we have already talked about this concept on our Blog, in the article Understanding Arrow Functions Once and For All.

We know that this is a feature that helps make our code leaner, and now it is also available in PHP. To create an arrow function, we need to use the reserved word fn, and then use the same syntax we are already used to in JavaScript. Let's see an example.

Imagine that we want to filter an array of numbers, returning only those that are less than 5. It is possible to do this with the array_filter() function. Let's use the following code:

$numbers = [1, 2, 3, 4, 5];

$numbers_filtered = array_filter($numbers, function ($n) {
    return ($n < 5);
});

This code will work without any problems. However, with arrow functions, we can perform the same function in just one line, with the following code:

$numbers_filtered = array_filter($numbers, fn($n) => ($n < 5));

Much more concise, don't you think?

However, it is important to highlight one important point. The concept of arrow functions in PHP is still not 100% the same as what we already know in JavaScript. For example, it is still not possible to define an arrow function as the value of a PHP variable. We can only use them, at least for now, in functions that expect another function as a parameter, such as array_filter(), array_map(), and array_reduce().

Null Coalescing Assignment Operator

The null coalescing operator was added to PHP in version 7.0 to simplify the use of the ternary operator with the isset() function. For example, imagine the following situation: we have a variable called $photo and we want to determine that if it has no value, we will set it to “default.png”. We can use the following code for this:

if (!isset($photo)) {
    $photo = "default.png";
}

However, using version 7.0, we can convert this if() into just one line, as we see below:

$photo = $photo ?? "default.png";

This operator is indeed very interesting. However, version 7.4 simplified this syntax even further. Note that in the code above we repeated the variable $photo call. From now on, if we encounter this situation, we just need to provide the two question marks and the equal sign together, like this:

$photo ??= "default.png";

Even simpler, isn't it?

Array Spread

This is another novelty that reminds us of JavaScript features. We can cautiously say that array spreading is like the “spread operator” in PHP. This is a novelty that also came to simplify our codes.

Let's see an example. Imagine we have two arrays: one called $companies and another called $old_companies. They have the following structure:

$companies = ['Facebook', 'Google'];
$old_companies = ['Apple', 'Microsoft', 'IBM'];

Now, imagine we want to merge these two arrays into one. You might already think of the array_merge() function. We could use it with this code:

$all_companies = array_merge($old_companies, $companies);

This code would work. However, with the array spread feature, we can implement the values of one array into another at the time of its creation. To do this, just use the three dots and the array name. The code above could be rewritten like this:

$companies = ['Facebook', 'Google'];
$all_companies = ['Apple', 'Microsoft', 'IBM', ...$companies];

Note that in this second case it was not even necessary to create the $old_companies array, which made our code a little cleaner.

Just remember that even though this novelty reminds us of the concept of the spread operator in JavaScript, it still does not have all the resources that ECMAScript has in this regard.

Numeric Separator

A very interesting novelty in PHP 7.4 is the visual separation of numeric values by using an underscore (_) without generating a syntax error.

How can this help us?

Let's create a variable called $number that will have a numeric value:

$number = 1000000;

Excellent! But what number is that? It’s hard to tell without visual separators, isn't it? With PHP 7.4, we can create the same variable this way:

$number = 1_000_000;

Now it’s easy to understand that this number is 1 million. Much more readable, isn't it? The best part is that this code will not return any errors in your text editor and if we display it on the screen in numeric format, using the code echo number_format($number, 0, ".", "."), we will see the following result:

Return of the code in our browser

Note that PHP identified the number as an integer and also added the separators in the correct places. This can also be done with variables of type float, hexadecimal, or binary.

The novelties of PHP 7.4 are indeed very relevant. And it’s great to follow the development and maturation of this language, which is always evolving. PHP 8 may be released later this year. What more new features will we see? Only time will tell.

In the meantime, we can continue studying and learning this amazing programming language, which helps us create incredible projects for the Web. Until the next article :)

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.