PHP quantity type and return value type declaration

Scalar type declaration

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

PHP 7 adds the characteristics of the scalar type declaration. There are two modes of the scale type declaration:

## This compulsory mode (default)

# Strict mode

Scalar type declaration syntax format:

declare(strict_types=1);

In code By specifying 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.

declare(strict_types=1) must be the first statement of the file. If this statement appears elsewhere in the file, a compilation error will be generated, and block mode is explicitly prohibited.

The strict_types directive affects the specified file and will not affect other files included by it (through include, etc.). This directive is compiled at runtime and cannot be modified. The way it works is to set a flag in the opcode so that function calls and return type checks comply with type constraints.

The type parameters that can be used are:

int

float

bool

string

interfaces

##array

callable

Force mode instance

Instance

<?php 
// 强制模式 
function sum(int ...$ints) 
{ 
   return array_sum($ints); 
} 
print(sum(2, '3', 4.1)); 
?>

The output result of the execution of the above program is:

9

#Instance summary Convert parameter 4.1 into an integer 4 and then add it.

Strict Mode Example

Example

<?php 
// 严格模式 
declare(strict_types=1); 
function sum(int ...$ints) 
{ 
   return array_sum($ints); 
} 
print(sum(2, '3', 4.1)); 
?>

The above program uses strict mode, so if an inappropriate integer appears in the parameter The type will report an error, and the execution output result is:

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

Return type declaration

PHP 7 adds support for return type declaration, which specifies the function return value type.

The return types that can be declared are:

int

float

bool

string

interfaces

##array

callable

Return type declaration instanceIn the instance, the return result is required to be an integer:

Instance

<?php 
declare(strict_types=1); 
function returnIntValue(int $value): int 
{ 
   return $value; 
} 
print(returnIntValue(5)); 
?>

The above program execution output result is:

5


Return type declaration error example

Example

<?php 
declare(strict_types=1); 
function returnIntValue(int $value): int 
{ 
   return $value + 1.0; 
} 
print(returnIntValue(5)); 
?>

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

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


Continuing Learning
||
<?php // 强制模式 function sum(int $ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); ?>
submitReset Code