用於比較2個表達式,例如當\(a小於,等於或大於\)b時,分別返回-1,0,1
php echo 1 <=> 1; //0 echo PHP_EOL; echo 1 <=> 2; //-1 echo PHP_EOL; echo 2 <=> 1; //1
php
declare(strict_types=1); //strict_types=1表示嚴格模式 function sum(int ...$ints){ return array_sum($ints); } var_dump(sum(1,'2','3.1',4.1));
Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given
若是關閉嚴格模式,傳入的參數會先轉換爲Int,結果爲10web
declare(strict_types=1); function sum(int ...$ints):int { return array_sum($ints); } var_dump(sum(1,2,3)); //6
php7.1對函數返回值類型作了擴充,可定義返回值類型爲void,不管是否開啓嚴格模式,==只要函數有"return;" 之外的其它return語句都會報錯==數組
declare(strict_types=1); function sum(int ...$ints):void { // return NULL; //報錯 return ; //後面有 "return;"以外的語句也報錯 // return array_sum($ints); } var_dump(sum(1,2,3));
php7.1對參數類型和返回值類型還有進一步的支持,其類型可使空類型,在參數或者返回值類型聲明前加上"?"表示要麼是null,要麼是聲明的類型php7
declare(strict_types=1); function test(?int $a):?int { return $a; } var_dump(test(null)); //NULL var_dump(test(1)); //int(1) var_dump(test('a')); // Fatal error
$page=isset($_GET['p'])?$_GET['p']:0; //php7 $page =$_GET['p'] ??0; //三元運算符 $page =$_GET['p'] ?? $_POST['p'] ??0;
php7以前沒法經過define來定義一個常量數組的,php7支持了這個操做閉包
define('STUDENT',['boy','girl']); print_r(STUDENT);
//php以前 use Space\ClassA; use Space\ClassB; use Space\ClassC C; //php7 use Space\{ClassA,ClassB,ClassC as C};
在php7以前,若是代碼中有語法錯誤或者fatal error時,程序會直接報錯退出,php7中實現了全局throwable接口,原來的Exception和部分Error實現了該接口函數
這種Error能夠像Exception同樣被第一個匹配的try/catch塊捕獲。若是沒有匹配的catch塊,則調用異常函數處理。若是還沒有註冊異常處理函數,則按照傳統方式處理(Fatal error)this
Error類並不是繼承自Exception,因此不能用catch(Exception $e){}來捕獲,可用catch(Error $e){} ,或者經過註冊異常處理函數(set_exception_handler())來捕獲Errorspa
try{ abc(); }catch (Error $e){ print_r($e); } echo 123;
Error Object ( [message:protected] => Call to undefined function abc() [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/hgfs/www/web/md.php [line:protected] => 4 [trace:Error:private] => Array ( ) [previous:Error:private] => ) 123
set_exception_handler(function($e){ echo "err:".print_r($e,true); }); abc(); echo 123;
php7下結果,php5依然Fatal error指針
err:Error Object ( [message:protected] => Call to undefined function abc() [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/hgfs/www/web/md.php [line:protected] => 7 [trace:Error:private] => Array ( ) [previous:Error:private] => ) 以後的也不會執行了
class People{ private $age=10; } $f=function(){ return $this->age+1; }; $p=$f->bindTo(new People,'People'); echo $p();
class People{ private $age=10; } $f=function(){ return $this->age+1; }; echo $f->call(new People);
echo intdiv(10,3);
$a=[1,2,3]; list($n1,$n2,$n3)=$a; //php7 [$n1,$n2,$n3]=[4,5,6]; //[]並非數組,而是list的簡略形式
$arr=[1,2,3,4,5,6]; foreach ($arr as $key => $value) { if($value ==2) break; } echo current($arr);//php7下 1,php5下 3
interface Cache{ public function read (); } class Ca { private $cache; public function setcache(Cache $cache){ $this->cache=$cache; } } $fcache =new Ca; $fcache->setcache(new Class implements Cache { public function read(){ } });
其它,移除ASP和script PHP標籤,移除$HTTP_RAW_POST_DATA、匿名類、常量類可見性等code