5 new features in PHP 7 need to know

After PHP 5 launched in 2004, from then until 2015 there was no major upgrade version of PHP, mainly patches, performance improvements and some new new features such as programming heaps of statues, … the PHP community is always looking forward to something truly innovative for PHP and by the end of December 2015 PHP 7 has officially launched with a host of new features after a long time of beta ( PHPNG). In this article, I will talk about 5 new points in PHP 7 that I think is very worthwhile.

1. PHP 7 gives 2 times faster speed than PHP 5

The speed in PHP 7 is dramatically improved when 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. The below speed benmark table will show you that, when testing on 2 famous CMS, it is Drupal and WordPress, it is not necessary to optimize what speed is 2 times faster, especially there are currently 25% of websites. running on WordPress, this is great for everyone.

php 7 has 2 times faster speed php5

2. PHP 7 allows declaring the type of variable

This feature in PHP7 is called Tyle Declarations, which allows the type declaration of variables instead of letting PHP automatically decide what type it used to be. Before PHP 7, it was still criticized for its poor language, weak typed language, but with this feature you can use to bind variables to a specific data type to make your code easier to read.

In PHP 5, you can also use the type hint feature to specify the desired data type of a parameter when declaring a function or method, but all stop at the declaration. When you call this function, PHP checks to see if the parameters match the specified type. If not, the compiler will report an error and the program will be paused. The type hint of PHP 5 is not only limited to use in function declarations but also limited to that type of class or an array. Here is an example:

For example with the enroll () function above, we can specify the first parameter as a student object and the second parameter is an array of classes. If you intentionally pass a string instead of an object, you will get an error message, if we only pass one class instead of an array of classes we will also receive an error message. Let's look at one more example below:

If the above code runs on PHP 5 it will report an error because as I mentioned above, type hint in php5 only allows 2 types of classes and arrays, passing as if you passed the string type and means the data type The variable is of type string but php5 understands this function is passed to an object of class string.

Scalar Type Hints

In section 2, I have said the type hint in PHP 5 only has two types: class and array but in PHP 7, it has added many more types: integer, float, string, bool. With this feature, PHP is no longer known as a language, but to use Scalar Type Hints, you must enable strict mode, I will say below, with Scalar Type Hint in PHP. 7, help our code write more closely and easier to read. Say anything, there must be an example:

TopDev Techtalk # 54: PHP & PHP7 – Secrets behind Optimization

* Ho Chi Minh: 18:00 – 21:00 on 12/01/2017

9

When Strict Mode is enabled, if the parameter passed is string, while we declare float, it will report Fatal error: Uncaught TypeError: Argument 2 passed to getTotal () phải là của kiểu float, chuỗi given.

If you pass in a number of int types, PHP will perform a procedure called "widening" and it will manually add ".0" at the end to become the float type.

PHP 7 allows declaring the return type

If you have programmed with languages ​​such as C, C ++, Java, … then you will be familiar with the syntax of declaring the function that is declaring the return type of the function before the function name. Back in PHP 7, it is not like that, but you can return any data type you want, which sometimes accidentally makes our function not tight, so PHP 7 has added The feature allows declaring the return type of the function, if the return type is false compared to the declaration, it will generate an error. To specify the return type for a function, you only need to declare a colon and then return the type immediately after the function name, before curly braces, for example:

Note: Non-strict Int in PHP 7

If we specify the return type to be int but strict mode is enabled, it is considered that we have not specified the return type, so if declaring the return type is int , you need to declare strict mode ( declare (strict_types = 1) )

Strict-int:

If in the function you specify the data type of the variable to be float, but the return type is int and has strict mode enabled, then PHP will report an error, so you need to cast, see the following example:

Top PHP & PHP7 optimization secrets. Sign up to find out now!
Top PHP & PHP7 optimization secrets. Sign up to find out now!

3. New operator

Spaceship Operator

PHP 7 provides some new operators, first of all the spaceship operator (<=>), which looks like a space shuttle so they name it. This is a 2-value comparison operator, to better understand you see the following example:

This operator writes starting with a lower sign, followed by an equal sign and larger sign (<=>), the operator will check if two equal values ​​will return 0, if the left value is less than the price On the right, return -1, and the left value is bigger than the right, it returns 1

Null Coalesce Operator

Another new operator is Null Coalesce, which is useful when you want to check if a value is NULL, this operator returns the left value if it is not NULL, otherwise it returns on the right, how to write this operator is 2 question marks?

In the above example, the variable $ name will receive the value of the variable $ firstName if it is not NULL, otherwise the variable $ name will receive the value "Guest".

Before PHP7, to do this you have to write the following:

This operator is also used with many values, in the following example it will check from left to right to see which value is not NULL then it will get that value for the variable $ name

4. Easy User-land CSPRNG

What is Easy User-land CSPRNG?

  User-land is a concept related to the workspace of the application, the software to the core of the system and protected by its own rights, it refers to the code that runs outside of the wallet system For example, PHP is an external software installed on the system and it operates unrelated to the kernel of the operating system, which is user-land. PHP 7 uses a concept that is CSPRNG ( C ryptographically S ecure P seudo R andom N umber G enerator)

You can see in PHP5 that there are some random functions like rand (), which are functions that generate random numbers, but the user-land of this function is only within PHP's scope, so it can be easy. Being hacked, securing these random functions is very important. In PHP 7, these random functions use the system interface for randomization, meaning that its user-land is no longer in PHP but in the kernel of the system, so even if PHP is hacked it is very It is difficult to hack into the system, especially since these random functions are used for random passwords.

In addition, PHP 7 adds two new random functions, random_int and random_bytes.

Random Bytes

When using random_bytes, you provide a unique parameter that is the length of the string to be returned, this function will return a string containing secure encrypted random bytes. If you use bin2hex (), you will get numbers like decimal.

These numbers are not integers, so if you want you have to use random_int

Random_int

When using this function you must provide 2 min and max parameters, which will return random numbers for this min, max, for example

If you're still using PHP5's rand () function, switch to random_int for more security.

5. Error Handling

The next feature that I think is very useful in PHP7 is Error Handling. Fatal Error is a very common error in PHP5 and is considered a fatal error and the translation program must stop recompiling. Sometimes it appears a blank screen that is easy to confuse to users. In PHP 7, the exception handling is improved, an exception will be generated when a Fatal error occurs, in PHP 5 it only stops the program and PHP 7 in addition to stopping the program also produces an exception.

In PHP 7, Error handling and exception handling are applied to a new class called Throwable, details about this you search Google.

Top PHP & PHP7 optimization secrets. Sign up to find out now!
Top PHP & PHP7 optimization secrets. Sign up to find out now!

Lỗi

A new feature in handling errors is Type Error. This is an instance of the Throwable class and it will generate when the parameter passed into the function does not match the declared data type, see if you see the example below to better understand:

Parse Error

This is also an instance of the Throwable class and it will generate when you include / require the file in PHP or when using the eval () function that contains syntax errors, you will see the following example:

A fairly common situation is when we design the login page, and you want to drag a file after the user logs in, if the file has a problem it will notify us of this file. need to be fixed, if there is no Parse Error, we may not know this error. Summary Error and Exception in PHP 7 has been greatly improved to help coders more effectively.

Conclude

There are many new features added to PHP 7 but I cannot list them all in this article, you can search Google yourself or read the PHP Manual documentation. Can tell some good things like:

PHP 7 allows creating standard Unicode emoticons like

In addition, PHP 7 removes quite a lot of old functions like mysql, exec functions, so if you are upgrading from PHP 5 to PHP 7, be careful of some old functions that can't be used, you can Find out more on the PHP Manual's Docs page. The lesson is long, I will end here, thank you for reading here: D, there is something to answer, please contact me via Facebook .

Source: ITZone via Vietnamlab

Share the news now