- 內(nèi)容:
· 返回值類(lèi)型聲明
· null合并運(yùn)算符
· declare
-
在php7中增加了返回值類(lèi)型聲明
示例1:
<?php function helloWorld():string { return("HelloWord"); } function helloWorld1():string { return(1); } var_dump(helloWorld());//string(9) "HelloWord" var_dump(helloWorld1());//string(1) "1" //需要注意的是 如果當(dāng)前設(shè)置為嚴(yán)格模式則會(huì)報(bào)錯(cuò)
-
null合并運(yùn)算符
- 如果變量存在且值不為NULL,它就會(huì)返回自身的值酌予,否則返回它的第二個(gè)操作數(shù)磺箕。
<?php $a = $_GET['a'] ?? 'a'; //等同于 $a = isset($_GET['a']) ? $_GET['a'] :'a'; //簡(jiǎn)單解釋就是: 當(dāng)$_GET['a'] 不存在時(shí)候則將 $a 賦值為 'a'; print_r('$a= '.$a); // 例如: // https://******/index.php?a=test // 輸出 $a= test // https://******/index.php // 輸出 $a= a
declare命令
例子:
declare 代碼段中的 statement 部分將被執(zhí)行——怎樣執(zhí)行以及執(zhí)行中有什么副作用出現(xiàn)取決于 directive 中設(shè)定的指令奖慌。
declare 結(jié)構(gòu)也可用于全局范圍,影響到其后的所有代碼(但如果有 declare 結(jié)構(gòu)的文件被其它文件包含松靡,則對(duì)包含它的父文件不起作用)简僧。
declare 調(diào)試內(nèi)部程序使用.
先簡(jiǎn)單說(shuō)明,declare這個(gè)函數(shù)支持ticks雕欺,函數(shù)表示記錄程序塊岛马,需配合register_tick_function 函數(shù)使用。ticks參數(shù)表示運(yùn)行多少語(yǔ)句調(diào)用一次register_tick_function的函數(shù)屠列。并且declare支持兩種寫(xiě)法:
declare(ticks = 1); 整個(gè)腳本
declare(ticks = 1) { 內(nèi)部的代碼做記錄
…
}
上述代碼除了 函數(shù)體內(nèi),外部都會(huì)被執(zhí)行,運(yùn)行可以看執(zhí)行次數(shù)和時(shí)間. 他跟適合做測(cè)試代碼段中每一步分的執(zhí)行時(shí)間 和執(zhí)行次數(shù).
declare 必須是全局的,放在程序外部.
不是所有語(yǔ)句都可計(jì)時(shí)啦逆。通常條件表達(dá)式和參數(shù)表達(dá)式都不可計(jì)時(shí)。
tick 代表一個(gè)事件笛洛,事件的定義是在register_tick_function夏志;事件的執(zhí)行頻率是在(ticks=3)。
表示事件頻率是執(zhí)行3個(gè)才記錄一次. microtime() 的打印時(shí)間.
//需要在文件開(kāi)頭
//strict_types指令只影響指定使用的文件撞蜂,不會(huì)影響被它包含(通過(guò)include等方式)進(jìn)來(lái)的其他文件
declare(strict_types=1);// 嚴(yán)格模式下
declare(strict_types=0);// 弱校驗(yàn)?zāi)J?
<?php
declare (ticks = 1); //這句這么寫(xiě)表示全局的腳本都做處理
function foo() { //注冊(cè)的函數(shù)
static $no;
$no++;
echo $no."======";
echo microtime()."\n";
}
register_tick_function("foo"); //注冊(cè)函數(shù)盲镶,后面可以跟第2個(gè)參數(shù),表示函數(shù)的參數(shù)
$a = 1;
for($i=0;$i<5;$i++) { //這里的循環(huán)也是語(yǔ)句蝌诡,會(huì)做一次判斷$i<5的判斷執(zhí)行
$b = 1;
}
?>
- 拓展延伸//設(shè)置declare(strict_types=1);
//strict_types.php <?php declare(strict_types=1);//強(qiáng)制模式 function helloWorld():string { return("HelloWord"); } function helloWorld1():string { return(1); } //var_dump(helloWorld());//輸出:string(9) "HelloWord" //var_dump(helloWorld1());//當(dāng)設(shè)置非強(qiáng)制模式時(shí)候輸出: string(1) "1" /* * 當(dāng)設(shè)置成 declare(strict_types=1); 時(shí)候如果返回值與返回值類(lèi)型聲明 不一致則會(huì)報(bào)錯(cuò)如下: * Fatal error: Uncaught TypeError: Return value of helloWorld1() must be of the type string, int returned * 當(dāng) declare(strict_types=1); 后設(shè)置有error_reporting(0)時(shí)候則不會(huì)輸出return值 */ // 文件包含測(cè)試: /* * 經(jīng)過(guò)測(cè)試:當(dāng)include其他文件時(shí)候如果被include的文件頭部不設(shè)置 declare(strict_types=1)時(shí)候不會(huì)受到本文件的影響 */ include_once "strict_types1.php"; var_dump(helloWorld3());//直接輸出:string(1) "3" 不會(huì)受到declare(strict_types=1) 的影響 /* 測(cè)試說(shuō)明: * 當(dāng)作為主文件使用declare函數(shù)溉贿,被引用的文件中沒(méi)有declare時(shí)候 * 調(diào)用被引用的文件中的函數(shù)時(shí)候 * 無(wú)論是在被引用的文件中進(jìn)行 function 的調(diào)用,還是 在本文件中進(jìn)行調(diào)用浦旱,都不會(huì)受到 declare(strict_types=1); 的影響 * 注明:strict_types指令只影響指定使用的文件宇色,不會(huì)影響被它包含(通過(guò)include等方式)進(jìn)來(lái)的其他文件 */ ?> //strict_types1.php 內(nèi)容如下 <?php function helloWorld3():string { return(3); } var_dump(helloWorld3());//直接輸出:string(1) "3"