Cool syntaxes when you work with PHP

Tram Ho

Here, I’m just talking in general through each version, not specifically, if you can’t use them, then check it out in which version can be used.

1. Starting from PHP7

Since the launch of PHP7, the speed in PHP 7 has improved amazingly as it is at least 2 times faster than PHP 5, if you know the optimization it is even faster, maybe up to 3-4 times

1. Declare the type of the variable and return type

In php5, we only have 2 type-hints that are class and array. As for PHP7 we have more styles like : integerfloatstringbool.

As you have seen, we can declare the types of variables and can specify the return type of the function.

In PHP7.4 it is even more supported:

2. New operator

  • Spaceship Operator: < = >

  • Null Coalesce Operator : ??. It is used to replace the ternary operation associated with the Isset() function.

3. Anonymous class

We have an Anonymous function like this:

In PHP7, we also have an Anonymous class:

Self invoking function (IIFE) ?

Sounds familiar, like in js

And it looks like we do the same in PHP :v

More and more PHP code looks like js code, isn’t it?

4. Arrow function

In PHP7.4 you can write more concise function with arrow function, it’s like js :v

More examples combining arrow function :

5. Spread Operator trong Array Expressions

You must have seen people using threedots (...) in js already? yes, php can do it too :v

Too cool, isn’t it?

At a glance, PHP 7 we will have the above called new, and most commonly used.

2. What’s new in PHP8?

PHP8 is a new update, it contains more optimization features and also increases processing speed slightly. Let’s take a look at some cool things on php8.

1. Named arguments

In PHP7, when setting a function with many parameters, usually the parameters between you just want to be default, but when using it, you must pass parameters to it if you use parameters after it: eg

Therefore, PHP8 gives us a so called Named arguments, which helps us to ignore the default, options parameters.

Just like that, it is enough to understand that we are passing 1 in $c, the rest keep default.

2.Attributes

Instead of PHPDoc annotations, you can now use structured metadata with PHP’s native syntax.

3.Constructor property promotion

In PHP7, when declaring a property of a class, we usually declare its scope on the constructor, and then set it in the constructor:

Looks pretty long, right? But fortunately, PHP8 keeps this short:

You can also use it like this:

4. Union types

When you declare the type of a variable, you can declare more than one type of that variable

Below will give us the variable, $number will receive 2 types int|float If it doesn’t match 1 of the 2, it will fail TypeError

5. Match

We often use switch cases, but each case in it is too simple, it makes its function longer.

With PHP8 you can use match, it will help the case of the switch more compact:

6. Nullsafe

If anyone uses js, then you can see that js is similar to this

 In PHP there is also similar to this:

7. Operators

In addition to operators like:

Conclude

The above are not new things in PHP7 and PHP8, these are just the ones I often use of each version, there are many new things that I have not mentioned, you can refer to them online. Thank you for watching, I hope you continue to support me.

Refer : https://b29.vn/bai-viet/nhung-cu-phap-hay-ho-khi-ban-lam-viec-voi-php?id=41

Share the news now