Master the new features of each version of PHP 7.x
New features of PHP 7.x in each version
Preface
Last month my colleague saw me writing
1 |
|
and asked me what this way of writing is, is there any other way of writing it? I said this is a writing method that is only available in PHP7 and above. Don’t you know? He said he didn't know.
I muttered in my heart and planned to start writing this blog.
PHP7 should be a modern PHP in addition to the basics. Because in PHP7, strong type definitions and some grammatical writing methods, such as combined comparison operators, define() can define arrays and other features. The formal introduction begins below, starting with PHP7.0. New versions will be added in the future, and they will be added one after another.
Ok, let’s start
Recommended (free): PHP7
PHP 7.0
Scalar type declaration
What is a scalar type?
Four scalar types:
boolean (Boolean type)
integer (integer type)
float (floating point type, also called double)
string (character String)
Two composite types:
array (array)
object (object)
Resource is a special variable that holds a reference to an external resource. Resources are created and used through specialized functions. Resource type variables are special handles for opening files, database connections, graphics canvas areas, etc.
To put it more simply, a scalar type is a data type that defines variables.
In php5, there are class names, interfaces, arrays and callback functions. In PHP, strings, integers, floats, and bools have been added. Let's take an example below. See the example for everything
1 2 |
|
Here, we define that $a must be of type int. If string is passed in the type function, an error will be reported. Let's modify the above code.
1 2 |
|
Return value type declaration
The method return value of the function can be defined. For example, a certain function of mine must return Int type, it will definitely return int. If you return string, an error will be reported. As follows
1 2 3 4 5 6 7 8 9 10 |
|
What happens when we define an array and return string or other types?
Then he willreport an errorFor example
1 2 3 4 5 6 7 8 9 10 11 |
|
null merge operator
Due to the fact that there are a large number of three simultaneous uses in daily use In the case of metaexpressions and isset(), we add the syntactic sugar of the null coalescing operator (??). If the variable exists and is not NULL, it returns its own value, otherwise it returns its second operand.
1 2 3 4 5 6 7 8 |
|
Spaceship operator
1 2 |
|
define Define array
In versions prior to PHP7, define cannot define arrays It is now possible, for example,
1 2 3 4 5 6 7 |
|
use method batch import
1 |
|
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Nullable types
The types of parameters and return values can now be passed in the type Add a question mark before it to allow it to be empty. When this feature is enabled, the parameters passed in or the result returned by the function are either of the given type or null .1 2 3 4 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 |
|
For example,
1 2 3 4 |
|
1 2 3 4 |
|
1 2 |
|
PHP7.3 There are no big changes at the syntax level.
PHP 7.4Class attributes support type declaration
Congratulations on PHP taking another step towards strong typing
1 2 3 |
|
Arrow functions provide a shorthand syntax for defining functions using implicit by-value scope binding. It’s similar to the arrow function of JS, but with an fn. A wave of complaints
1 |
|
If you like it, click "Follow". ######
1
2
<?php
$array
['key'] ??= computeDefault();
// 类似与这个if (!isset($array['key'])) {
$array
['key'] = computeDefault();}?>
Copy after login
I am a farmer, a coder who usually writes and pastes code.
The above is the detailed content of Master the new features of each version of PHP 7.x. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

