PHP7 functions, declarations, return values and other new features introduced

Source: Internet
Author: User
Tags scalar
use ... Operators define variable-length parametric functions (PHP 5 >= 5.6.0, PHP 7)

Now you can not rely on Func_get_args (), using ... operator to implement variable-length parametric functions.

<?phpfunction f ($req, $opt = null, ... $params) {//$params is an array containing the remaining parameters printf (' $req:%d; $opt:%d; number of the params: %d '. " \ n ",   $req, $opt, Count ($params));} f (1); F (1, 2); F (1, 2, 3); F (1, 2, 3, 4); F (1, 2, 3, 4, 5);? >

The above routines will output:

$req: 1; $opt: 0; Number of params:0
$req: 1; $opt: 2; Number of params:0
$req: 1; $opt: 2; Number of Params:1
$req: 1; $opt: 2; Number of Params:2
$req: 1; $opt: 2; Number of Params:3

Use... Operator for parameter expansion (PHP 5 >= 5.6.0, PHP 7)

When calling a function, use the ... operator to expand the array and the traversed object to function parameters. In other programming languages, such as Ruby, this is called the join operator.

<?phpfunction Add ($a, $b, $c) {return $a + $b + $c;} $operators = [2, 3];echo Add (1, ... $operators);? >

The above routines will output:

6

Use function and use const (PHP 5 >= 5.6.0, PHP 7)

The use operator is extended to support the import of external functions and constants in a class. The corresponding structure is the use function and the use Const.

<?phpnamespace name\space {Const FOO = A, function f () {echo function. \ n "; }}namespace {Use const Name\space\foo; use function name\space\f; echo FOO. " \ n "; f ();}? >

The above routines will output:

42

Name\space\f

Debuginfo () (PHP 5 >= 5.6.0, PHP 7)

adding Debuginfo (), when using the Var_dump () output object, can be used to control the properties and values to be output.

<?phpclass C {private $prop; public function construct ($val) {  $this->prop = $val;} Public Function Debuginfo ( {  return [   ' propsquared ' = ' $this->prop * * 2,  ];} Var_dump (New C (42));? >

The above routines will output:

Object (C) #1 (1) {["propsquared"]=> int (1764)}

Scalar type declaration (PHP 7)

Scalar type declarations have two modes: 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 routines 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)

return value type declaration (PHP 7)

PHP 7 adds support for return type declarations. 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 above routines will output:

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

Null merge operator (PHP 7)

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.

<?php//fetches the value of $_get[' user ' and returns ' nobody '//if it does not exist. $username = $_get[' user ']?? ' nobody ';//This was equivalent to: $username = isset ($_get[' user ')? $_get[' user ': ' nobody ';//coalesces can be chained:this would return the first//defined value out of $_get[' user '], $_p ost[' user ', and//' nobody '. $username = $_get[' user ']?? $_post[' user '?? ' Nobody ';? >

Spaceship operator (combination comparator) (PHP 7)

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//Integersecho 1 <=> 1; 0echo 1 <=> 2; -1echo 2 <=> 1; 1//Floatsecho 1.5 <=> 1.5; 0echo 1.5 <=> 2.5; -1echo 2.5 <=> 1.5; 1//Stringsecho "a" <=> "a"; 0echo "A" <=> "B"; -1echo "B" <=> "a"; 1?>

Defining a constant array by define () (PHP 7)

Constants of type Array can now be defined by define (). can only be defined by const in PHP5.6.

<?phpdefine (' ANIMALS ', [' dog ', ' cat ', ' bird '), Echo animals[1]; Outputs "Cat"?>

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.