Summary of newly added features in PHP7 (with code)

Source: Internet
Author: User
Tags scalar
This article brings to you the content is about the new additions to the characteristics of the summary (with code), there is a certain reference value, the need for friends can refer to, I hope to help you. PHP7

1. Scalar type declarations

A) There are two modes of scalar type declaration: Mandatory (default) and strict mode. You can now use the following type parameters, whether in forced or strict mode: string (String), Integer (int), floating-point number (float), and Boolean (bool). They augment the other types introduced in PHP5: Class name, interface, array, and callback type.

<?php//coercive modefunction sumofints (int ... $ints) {    return array_sum ($ints);} Var_dump (sumofints (2, ' 3 ', 4.1));

The above results will output: Int (9)
To use strict mode, a declare declaration directive must be placed at the top of the file. This means that the scalar is strictly declared to be file-based. This directive affects not only the type declaration of the parameter, but also the return value declaration of the function (see return value type declaration, built-in PHP functions, and PHP functions loaded in the extension)

2. return type declaration

A) PHP 7 adds support for the return type declaration. Similar to a parameter type declaration, the return type declaration indicates the type of the function return value. The available types are the same as the types available in the parameter declaration.

<?phpfunction arrayssum (Array ... $arrays): array{    return Array_map (function (array $array): int {        return Array_sum ($array);    }, $arrays);} Print_r ([Arrayssum], [4,5,6], [7,8,9]);

The output is:

Array (    [0] = 6    [1] =    [2] = 24)

3.null Merge Operators

A) because of the large number of simultaneous use of ternary expressions and isset () in daily use, we added the syntax sugar of the null merge operator (??). If a variable exists and the value is not NULL, it returns its own value, otherwise it returns its second operand.

$username = $_get[' user_name '?? ' Nobody ';

4. Spaceship operator (combination comparator)

A) The spaceship operator is used to compare two expressions. When $ A is less than, equal to, or greater than $b, it returns-1, 0, or 1, respectively. The principle of comparison is to follow the regular comparison rules of PHP.

<?php//integer echo 1 <=> 1;//0echo 1 <=> 2;// -1echo 2 <=> 1;// 1//floating point echo 1.5 <=> 1.5; 0echo 1.5 <=> 2.5; -1echo 2.5 <=> 1.5; 1//String echo "a" <=> "a"; 0echo "A" <=> "B"; -1echo "B" <=> "a"; 1?> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.