search
HomeheadlinesIntroduction to new features in PHP7

Introduction to new features in PHP7

Jan 13, 2018 pm 05:12 PM
phpphp7Introduction

In this article, we mainly share with you the new features in PHP7 that we should learn and use, hoping to help everyone.

PHP7 was officially released in November 2015. This update can be said to be an important milestone for PHP. It will bring significant performance improvements and new features, and improve some features of the previous version. The editor of this article will work with you to understand and discuss the new features in PHP7.

1. Scalar type declaration

We know that PHP is a weakly typed programming language, so it does not provide any method to specify the type of input parameters and return values. PHP7 breaks this status quo and adds support for scalar types (int , float, string, bool) declaration support, add the declare(strict_types=1) instruction to declare whether strict type checking, let’s look at a piece of code:

(strict_types=) { $x + $y;} add(, );

Valid types are: class/interface name, self, array, callable, bool, float, int and string.

?? ——NULL merge Operator

The NULL coalescing operator has been added to PHP7. Don’t underestimate this “??”. With it, we can easily obtain a parameter and provide a parameter when it is empty. default value. How does the ?? operator return the left side if the value on its left side exists and is not NULL, otherwise its right side value will be returned. Let’s experience the power of the ?? operator through the following piece of code.

<?php
 // 获取user参数的值(如果为空,则用&#39;nobody&#39;)
 // PHP5中我们这样来实现: $username = isset($_GET[&#39;user&#39;]) ? $_GET[&#39;user&#39;] : &#39;nobody&#39;;
 // PHP7中,使用??运算符更便捷: $username = $_GET[&#39;user&#39;] ?? &#39;nobody&#39;; 
?>

Anonymous class

As the name suggests, an anonymous class does not have a class name, and its declaration and instantiation are at the same time. PHP7 supports instantiating an anonymous class through new class, which can be used to replace some " Complete class definition of "Incineration".

echo ( {     {       ;    }})->myMethod();

More Errors can be handled with exceptions

More Errors in PHP7 become catchable Exceptions and are returned to the developer. If they are not caught, they are Errors. If they are caught, they become Errors. It is an Exception that can be handled within the program. By default, Error will directly cause the program to interrupt, while PHP7 captures and handles it through try/catch blocks, allowing the program to continue executing, providing programmers with more flexible options.

Code example:

nonExistFunction($arg);

At this time, the above code will prompt the error "Fatal error: Call to a member function method() on a non-object", and this fatal error will stop the following Code execution continues.

So if you want to continue executing the code, you can solve it through exception handling:

{    nonExistFunction($arg);  }  (EngineException $e ) {     ;}

Combined with the comparison operator ()

Not much to explain , let’s look directly at the sample code. You can easily understand the function of this operator through the code.

{    ($a < $b) ?  : (($a > $b) ?  : )}  {    $a <=> $b;}

Define array constants

In the past, when we used define() to define constants, the data type only supported scalars, but in PHP7, it supports defining constants of array types.

define(&#39;MYCONSTANT&#39;, array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;))

There are many new features in PHP7. Today we will introduce these first. We will continue to update them in the future. We also welcome additions from PHPers. Let’s communicate, learn and make progress together.

Related recommendations:

Detailed explanation of the key to doubling the performance of PHP7

Methods to build a performance testing environment for PHP7

What changes have taken place from PHP5.3 to PHP7.1

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software