PHP7 has added the feature of scalar type declaration. There are two modes for scalar type declaration:
Forced mode (default)
strict Model
1. Syntax format:
declare( strict_types=1 );//代码中通过制定 strict_types 的值( 1 或者 0 )
2. Usage
Usedeclare(strict_types=1) to set whether to enable strict matching mode. After it is enabled , if the parameter passed in is not a preset parameter type, an error will be reported, for example:
<?php declare(strict_types=1); function test(int $param) { var_dump($param); } test("1");//会提示报错
declare(strict_types=0) to set whether to enable the forced mode. After it is enabled, if If the parameter entered is not a preset parameter type, no error will be reported, for example:
<?php declare(strict_types=1); function test(int $param) { var_dump($param); } test("1");//不会提示报错
3. Available types
The above is the detailed content of New features of php7: scalar type declaration. For more information, please follow other related articles on the PHP Chinese website!