Nullable type
Nullable type is mainly used for parameter type declaration and function return value declaration.
The main two forms are as follows:
<?phpfunction answer(): ?int { return null; //ok}function answer(): ?int { return 42; // ok}function say(?string $msg) { if ($msg) { echo $msg; }}
It is easy to understand from the example. What it means is to use the form of ? to indicate that the type of the function parameter or return value is either a specified type or null .
This method can also be used for the definition of interface functions:
<?php interface Fooable { function foo(?Fooable $f);}
But there is one thing to note: if the function itself defines the parameter type and has no default value, even if it is nullable , cannot be omitted, otherwise an error will be triggered. As follows:
<?php function foo_nullable(?Bar $bar) {} foo_nullable(new Bar); // 可行foo_nullable(null); // 可行foo_nullable(); // 不可行
But if the parameters of the above function are defined in the form of ?Bar $bar = null, the third way of writing is also feasible. Because = null is actually equivalent to a superset of ? , for nullable type parameters, null can be set as the default value.
The square bracket abbreviation of list
We know that before PHP5.4, arrays could only be defined through array(), which was added after 5.4 [] is a simplified way of writing (it is still very practical to omit 5 characters).
<?php // 5.4 之前$array = array(1, 2, 3);$array = array("a" => 1, "b" => 2, "c" => 3); // 5.4 及之后$array = [1, 2, 3];$array = ["a" => 1, "b" => 2, "c" => 3];
Extended to another question, if we want to assign the value of the array to different variables, it can be achieved through list:
<?php list($a, $b, $c) = $array;
Can it also be achieved through the abbreviation of [] Woolen cloth?
<?php [$a, $b, $c] = $array;
And the list specified key mentioned in the next feature:
<?php ["a" => $a, "b" => $b, "c" => $c] = $array;
PHP7.1 implements this feature. But it should be noted that the [] that appears in the lvalue is not the abbreviation of array, but the abbreviation of list().
But that’s not all, the new list() implementation can not only appear in lvalues, but can also be used in foreach loops:
<?php foreach ($points as ["x" => $x, "y" => $y]) { var_dump($x, $y);
However, due to implementation issues, list() and [] cannot be used nested in each other:
<?php // 不合法 list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]]; // 不合法 [list($a, $b), list($c, $d)] = [[1, 2], [3, 4]]; // 合法 [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];
Allows the key to be specified in the list
As mentioned above, new The key can be specified in the implementation of list():
<?php $array = ["a" => 1, "b" => 2, "c" => 3];["a" => $a, "b" => $b, "c" => $c] = $array;
This is equivalent to:
<?php $a = $array['a'];$b = $array['b'];$c = $array['c'];
The difference from the past is that the previous implementation of list() is equivalent to the key being only 0 , 1, 2, 3 and the order cannot be adjusted. Execute the following statement:
<?php list($a, $b) = [1 => '1', 2 => '2'];
will get the error PHP error: Undefined offset: 0... .
The new implementation can adjust the assignment in the following ways:
<?php list(1 => $a, 2 => $b) = [1 => '1', 2 => '2'];
Unlike arrays, list does not support mixed keys. The following writing will trigger a parsing error. :
<?php // Parse error: syntax error, ...list($unkeyed, "key" => $keyed) = $array;
For more complex situations, list also supports composite form parsing:
<?php $points = [ ["x" => 1, "y" => 2], ["x" => 2, "y" => 1]]; list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points; $points = [ "first" => [1, 2], "second" => [2, 1]]; list("first" => list($x1, $y1), "second" => list($x2, $y2)) = $points;
and used in loops:
<?php $points = [ ["x" => 1, "y" => 2], ["x" => 2, "y" => 1]]; foreach ($points as list("x" => $x, "y" => $y)) { echo "Point at ($x, $y)", PHP_EOL;}
void return type
PHP7 .0 adds the feature of specifying function return type, but the return type cannot be specified as void. This feature of 7.1 is a supplement:
<?php function should_return_nothing(): void { return 1; // Fatal error: A void function must not return a value}
The following two situations can be verified:
<?php function lacks_return(): void { // valid} function returns_nothing(): void { return; // valid}
A function defined with a return type of void cannot have a return value, even if it returns null:
<?php function returns_one(): void { return 1; // Fatal error: A void function must not return a value} function returns_null(): void { return null; // Fatal error: A void function must not return a value}
In addition, void is only applicable to the return type and cannot be used for parameter type declaration, or it will trigger an error:
<?php function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type}
The declaration of return type in a class function cannot be overridden by a subclass, otherwise an error will be triggered:
<?php class Foo{ public function bar(): void { } } class Foobar extends Foo{ public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void }}
Class constant attribute setting
This feature It is relatively simple to say, that is, the constants in the class now support the use of public, private and protected modifications:
<?php class Token { // 常量默认为 public const PUBLIC_CONST = 0; // 可以自定义常量的可见范围 private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0; // 多个常量同时声明只能有一个属性 private const FOO = 1, BAR = 2;}
In addition, the constants in the interface (interface) can only be public attributes:
<?php interface ICache { public const PUBLIC = 0; const IMPLICIT_PUBLIC = 1;}
In order to cope with the changes, the implementation of the reflection class has also been enriched accordingly, and two methods getReflectionConstant and getReflectionConstants have been added to obtain additional attributes of constants:
<?php class testClass { const TEST_CONST = 'test'; } $obj = new ReflectionClass( "testClass" ); $const = $obj->getReflectionConstant( "TEST_CONST" ); $consts = $obj->getReflectionConstants();
Multi-condition catch
In the past In the try...catch statement, each catch can only set one conditional judgment:
<?php try { // Some code... } catch (ExceptionType1 $e) { // 处理 ExceptionType1} catch (ExceptionType2 $e) { // 处理 ExceptionType2} catch (Exception $e) { // ...}
In the new implementation, multiple conditions can be set in one catch, which is equivalent to the form judgment of or:
<?php try { // Some code...} catch (ExceptionType1 | ExceptionType2 $e) { // 对于 ExceptionType1 和 ExceptionType2 的处理} catch (Exception $e) { // ...}
The above is the list of new features of PHP 7.1. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

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