1. null合并運(yùn)算符(??)
語法: 如果變量存在且值不為NULL,它就會返回自身的值,否則返回它的第二個操作數(shù).
//php7以前 if判斷
if(empty($_GET['param'])) {
$param = 1;
}else{
$param = $_GET['param'];
}
//php7以前 三元運(yùn)算符
$param = empty($_GET['param']) ? 1 : $_GET['param'];
//PHP7 null合并運(yùn)算符
$param = $_GET['param'] ?? 1;//1
2. define() 定義常量數(shù)組
//php7以前
define("CONTENT", "hello world");
echo CONTENT;//hello world
//PHP7
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[2];//bird
//PHP7 類外也可使用const來定義常量
const CONSTANT = 'Hello World';
echo CONSTANT;//Hello World
3. 組合比較符(=>)
組合比較符用于比較兩個表達(dá)式.當(dāng)$a小于、等于或大于$b時它分別返回-1、0或1. 比較的原則是沿用PHP的常規(guī)比較規(guī)則進(jìn)行的.
/整數(shù)
echo 1 => 1; // 0
echo 1 => 2; // -1
echo 2 => 1; // 1
//浮點(diǎn)數(shù)
echo 1.5 => 1.5; // 0
echo 1.5 => 2.5; // -1
echo 2.5 => 1.5; // 1
//字符串
echo "a" => "a"; // 0
echo "a" => "b"; // -1
echo "b" => "a"; // 1
4. 變量類型聲明
兩種模式: 強(qiáng)制(默認(rèn))和嚴(yán)格模式. 可以使用下列類型參數(shù): string,int,float,bool
//... 操作符: 表示這是一個可變參數(shù). php5.6及以上的版本可使用: 函數(shù)定義的時候變量前使用.
function intSum(int ...$ints){
return array_sum($ints);
}
var_dump(intSum(2,'3.5'));//5
//嚴(yán)格模式
//模式聲明:declare(strict_types=1); 默認(rèn)情況值為0,值為1代表為嚴(yán)格校驗(yàn)的模式
declare(strict_types=1);
function add(int $a,int $b){
return $a+$b;
}
var_dump(add(2,'3.5')); //Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type integer
5. 返回值類型聲明
增加返回類型聲明的支持.類似于參數(shù)類型聲明.(用法在函數(shù)定義的后面加 :類型名)
//有效的返回類型
declare(strict_types = 1);
function getInt(int $value): int {
return $value;
}
print(getInt(6));//6
//無效返回類型
declare(strict_types = 1);
function getNoInt(int $value): int {
return $value+'2.5';
}
print(getNoInt(6));//Fatal error: Uncaught TypeError: Return value of getNoInt() must be of the type integer
6. 匿名類
允許new class {} 創(chuàng)建一個匿名的對象.
?php
//php7以前 接口實(shí)現(xiàn)
interface User{
public function getDiscount();
}
class VipUser implements User{
//折扣系數(shù)
private $discount = 0.6;
public function getDiscount() {
return $this->discount;
}
}
class Goods{
private $price = 200;
private $objectVipUser;
//User接口VipUser類實(shí)現(xiàn)
public function getUserData($User){
$this->objectVipUser = $User;
$discount = $this->objectVipUser->getDiscount();
echo "商品價格:".$this->price*$discount;
}
}
$display = new Goods();
//常規(guī)實(shí)例化接口實(shí)現(xiàn)對象
$display ->getUserData(new VipUser);//商品價格:120
?php
//php7 創(chuàng)建一個匿名的對象
interface User{
public function getDiscount();
}
class Goods{
private $price = 200;
private $objectVipUser;
public function getUserData($User){
$this->objectVipUser = $User;
$discount = $this->objectVipUser->getDiscount();
echo "商品價格:".$this->price*$discount;
}
}
$display = new Goods();
//new匿名對象實(shí)現(xiàn)user接口
$display ->getUserData(new class implements User{
private $discount = 0.6;
public function getDiscount() {
return $this->discount;
}
});//商品價格:120
7. Closure::call()
Closure::call() 方法被添加為一個簡短的方式來臨時綁定一個對象作用域到一個閉包并調(diào)用它. 與PHP5的bindTo相比.它的性能要快得多.
?php
//php7以前
class A {
private $attribute = 'hello world';
}
$getClosure = function(){
return $this->attribute;
};
$getAttribute = $getClosure->bindTo(new A, 'A');//中間層閉包
echo $getAttribute();//hello world
?php
//PHP7
class A {
private $attribute = 'hello world';
}
$getClosure = function(){
return $this->attribute;
};
echo $getClosure->call(new A);//hello world
8. unserialize()
unserialize()函數(shù):過濾的特性,可以防止非法數(shù)據(jù)進(jìn)行代碼注入,提供了更安全的反序列化數(shù)據(jù)
?php
class A{
public $name = 'admin_a';
}
class B{
public $name = 'admin_b';
}
$objA = new A();
$objB = new B();
$serializedObjA = serialize($objA);
$serializedObjB = serialize($objB);
//默認(rèn)行為是接收所有類; 第二個參數(shù)可以忽略
$dataA = unserialize($serializedObjA , ["allowed_classes" => true]);
var_dump($dataA);//object(A)#3 (1) { ["name"]=> string(7) "admin_a" }
//如果allowed_classes設(shè)置為false,unserialize會將所有對象轉(zhuǎn)換為__PHP_Incomplete_Class對象
$dataA = unserialize($serializedObjA , ["allowed_classes" => false]);
var_dump($dataA);//object(__PHP_Incomplete_Class)#4 (2) { ["__PHP_Incomplete_Class_Name"]=> string(1) "A" ["name"]=> string(7) "admin_a" }
//轉(zhuǎn)換所有對象到 __PHP_Incomplete_Class對象,除了對象"B"
$dataB = unserialize($serializedObjB , ["allowed_classes" => ["B"]]);
var_dump($dataB);//object(B)#3 (1) { ["name"]=> string(7) "admin_b" }
9. IntlChar
IntlChar:提供了一些可用于訪問Unicode字符信息的實(shí)用方法的訪問. 注意:必須安裝Intl擴(kuò)展才能使用!
var_dump(IntlChar::CODEPOINT_MAX);//int(1114111)
echo 'br>';
var_dump(IntlChar::charName('+'));//string(9) "PLUS SIGN"
echo 'br>';
var_dump(IntlChar::ispunct('?'));//bool(true)
10. CSPRNG
CSPRNG 函數(shù)提供一種簡單的機(jī)制來生成密碼的隨機(jī)數(shù).
random_bytes() -加密生存被保護(hù)的偽隨機(jī)字符串.
random_int() -加密生存被保護(hù)的偽隨機(jī)整數(shù).
$bytes = random_bytes(8);
echo(bin2hex($bytes));//隨機(jī)2073a110a2e3c497
echo 'br>';
echo(random_int(1, 999));//隨機(jī)786
echo 'br>';
print(random_int(-999, -1));//隨機(jī)-357
11. use 語句
可以使用單個use語句從相同的命名空間導(dǎo)入類,函數(shù)和常量,而不是使用多個use語句.
//PHP7之前
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP7之后
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
12. intp
新增加intp()函數(shù),接收兩個參數(shù),返回值為第一個參數(shù)除于第二個參數(shù)的值并取整.
echo intp(8,4);//2
echo intp(10,4);//2
echo intp(5,10);//0
13. PHP7 錯誤處理
PHP7 改變了大多數(shù)錯誤的報告方式.不同于PHP5的傳統(tǒng)錯誤報告機(jī)制,現(xiàn)在大多數(shù)錯誤被作為Error異常拋出.
這種Error異??梢韵衿胀ó惓R粯颖籺ry / catch塊所捕獲. 如果沒有匹配的try / catch塊,則調(diào)用異常處理函數(shù)(由 set_exception_handler() 注冊)進(jìn)行處理.
如果尚未注冊異常處理函數(shù),則按照傳統(tǒng)方式處理:被報告為一個致命錯誤(Fatal Error).
Error類并不是從Exception類擴(kuò)展出來的,所以用catch (Exception $e) { ... } 這樣的代碼是捕獲不到Error的.你可以用 catch (Error $e) { ... } 這樣的代碼,
或者通過注冊異常處理函數(shù)( set_exception_handler())來捕獲Error.

?php
//php7以前 自定義異常處理
class getException extends Exception{
public function errorMsg(){
return '錯誤的信息'.$this->getMessage().'br>錯誤的代碼'.$this->getCode();
}
}
try {
$num =10;
if($num > 1) {
throw new getException($num,404);
}
} catch (getException $e) {
echo $e->errorMsg();
}
?php
//php7 異常處理
try {
test();
}catch(Error $e) {
echo $e->getMessage();//Call to undefined function test()
}
您可能感興趣的文章:- 簡述PHP7.4 新特性和廢棄的功能
- php5.6.x到php7.0.x特性小結(jié)
- PHP7新特性之抽象語法樹(AST)帶來的變化詳解
- php7函數(shù),聲明,返回值等新特性介紹
- PHP7新特性簡述
- PHP7 新特性詳細(xì)介紹
- PHP7新特性foreach 修改示例介紹
- 淺談php7的重大新特性
- PHP7中新添特性整理