PHP Scalar Type Declarations and Return Type Declarations

PHP 7 新特性PHP 7 New Features


Scalar Type Declarations

By default, all PHP files are in weak type checking mode.

PHP 7 adds the feature of scalar type declarations. There are two modes for scalar type declarations:

  • Coercive mode (default)
  • Strict mode

Scalar type declaration syntax format:

declare(strict_types=1); 

In the code, specify the value of strict_types (1 or 0). 1 indicates strict type checking mode, which applies to function calls and return statements; 0 indicates weak type checking mode.

The type parameters that can be used are:

  • int

  • float

  • bool

  • string

  • interfaces

  • array

  • callable

Example of Coercive Mode

Example

<?php
// Coercive mode
function sum(int ...$ints)
{
   return
array_sum($ints);
}

print(
sum(2, '3', 4.1));
?>

The output of the above program is:

9

In the example, the parameter 4.1 is converted to integer 4 before being added.

Example of Strict Mode

Example

<?php
// Strict mode
declare(strict_types=1);

function
sum(int ...$ints)
{
   return
array_sum($ints);
}

print(
sum(2, '3', 4.1));
?>

Because the above program uses strict mode, an error will be reported if a non-integer type appears in the parameter. The execution output is:

PHP Fatal error:  Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, called in……

Return Type Declarations

PHP 7 adds support for return type declarations. Return type declarations specify the type of a function's return value.

The return types that can be declared are:

  • int

  • float

  • bool

  • string

  • interfaces

  • array

  • callable

Example of Return Type Declarations

In the example, the return result is required to be an integer:

Example

<?php
declare(strict_types=1);

function
returnIntValue(int $value): int
{
   return
$value;
}

print(
returnIntValue(5));
?>

The output of the above program is:

5

Example of Return Type Declaration Error

Example

<?php
declare(strict_types=1);

function
returnIntValue(int $value): int
{
   return
$value + 1.0;
}

print(
returnIntValue(5));
?>

Because the above program uses strict mode, the return value must be int, but the calculation result is float, so an error will be reported. The execution output is:

Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned...

void Functions

A new return value type, void, is introduced. Methods whose return type is declared as void either simply omit the return statement, or use an empty return statement. For void functions, NULL is not a valid return value.

The return types also include void. Functions with a return type declared as void cannot have a return value, not even returning null.

void functions can omit the return statement, or use an empty return statement.

Example

<?php
function swap(&$left, &$right) : void
{
    if ($left === $right) {
        return;
    }

    $tmp = $left;
    $left = $right;
    $right = $tmp;
}

$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b);

The output of the above example is:

null
int(2)
int(1)

PHP 7 新特性PHP 7 New Features

Other Extensions