-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphp7_1.php
More file actions
65 lines (52 loc) · 1 KB
/
php7_1.php
File metadata and controls
65 lines (52 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* php7新特性——标量类型与返回值类型声明
*
*
*/
//默认严格模式,自动将转换数据类型
//declare(strict_types=0);
//1 表示强制模式,错误的数据类型直接报fatal error
static $c=1;
$d=3;
$res1 = test(1.5,2);
echo $res1,"\n";
$res2 = test(1.5,2);
echo $res2,"\n";
/*
* 输出结果
* 7 此时$d=5.5
* 8 因为返回值转为int,因此8.5变成了8
*/
/**
* @param int $a 输入参数自动转为int
* @param int $b
* @return int 返回值自动转为int
*/
function test(int $a, int $b) : int {
static $d = 4;
$res = $a +$b;
if (isset($c)) {
$res += $c;
}
if (isset($d)) {
$res +=$d;
$d += 1.5;
}
return $res;
}
##### 特殊的返回值类型 void
/**
* 返回值类型void 表示无返回值,null也不行
* @param $left
* @param $right
*/
function swap(&$left, &$right) : void
{
if ($left === $right) {
return;
}
$tmp = $left;
$left = $right;
$right = $tmp;
}