PHP7 scalar Type Declare usage example analysis _php skill

Source: Internet
Author: User
Tags scalar

The examples in this article describe the PHP7 scalar type declare usage. Share to everyone for your reference, specific as follows:

In order to improve the execution efficiency, the PHP7 of scalar type (Boolean, floating-point, Integer, character) is added to the function method, which saves the detection of data type.

PHP7 still supports weak type detection, which means that formal parameters can still be declared in the original way.

Scalar declarations have two characteristics:

Force mode (default): reflected on type conversions

Strict mode

Mode declaration: Declare (Strict_types=1);

The default is 0, and the value 1 represents the Strictly validated mode

Type parameters that can be used:

Int-float-bool-string-interfaces-array-callable

Action on the form participation return value type description, optional

Formal parameters

Forced mode
<?php
/**
 * Created by Phpstorm.
 * User:bee
 * DATE:2016/4/22 * time:10:17//
 PHP7 prior to the
Declaration mode
Function Type_weak (... $int) {
  Return Array_sum ($int);
Forced mode PHP7 Declaration mode
//force mode converts all arguments to integer
function sum (int ... $ints)
{
  //array_sum () Returns the value of all the values in an array, with the result of an integer or floating-point number.
  Print_r ($ints);
  echo "<br>";
  Return Array_sum ($ints);
Echo type_weak (2, ' 3 ', 0.11);
echo " 
 

The Operation effect chart is as follows:

Declare the mode as strict mode
<?php
/**
 * Created by Phpstorm.
 * User:bee
 * DATE:2016/4/22 * time:10:17 * *
 //declare must be in the first document
declare (Strict_types=1);
Force mode (default)
function Type_weak (... $int) {return
  array_sum ($int);
}
The force mode
function sum (int ... $ints)
{//array_sum () returns the sum of all the values in the array, with the result of an
  integer or floating-point number.
  Print_r ($ints);
  echo "<br>";
  Return Array_sum ($ints);
Echo type_weak (2, ' 3 ', 0.11);
echo " 
 

The Operation effect chart is as follows:

return value

<?php
/**
 * Created by Phpstorm.
 * User:bee *
 DATE:2016/4/22
 * time:10:17 * *
declare (strict_types=0);
Force mode (default)
function Type_weak (... $int): int{return
  array_sum ($int);
}
Force mode
function sum (int ... $ints): int
{
  //array_sum () returns the sum of all the values in the array with the result of an integer or floating-point number.
  Print_r ($ints);
  echo "<br>";
  Error in strict mode return
  array_sum ($ints) +0.6;
}
Echo type_weak (2, ' 3 ', 0.11);
echo " 
 

The Operation effect chart is as follows:

More interested in PHP related content readers can view the site topics: "Introduction to PHP Basic Grammar", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.