我整理的PHP 7.0主要新特性_php執行個體

來源:互聯網
上載者:User
截止到目前為止,PHP官方已經發布了php7的RC5版本,預計在11月份左右會發布第一個正式版本!現在來說php7的重大特性肯定已經是定型了,不會再有什麼變動了。後續一些版本的迭代主要也就是修修bug,最佳化之類的。下面就來說話我們一直期待的php7.0新特徵吧。

1.標量參數型別宣告

現在支援字串(string)、整型(int)、浮點數(float)、及布爾型(bool)參數聲明,以前只支援類名、介面、數組及Callable
兩種風格:強制轉換模式(預設)與strict 模式

<?php// Coercive modefunction sumOfInts(int ...$ints){return array_sum($ints);}var_dump(sumOfInts(2, '3', 4.1)); 

2.傳回型別聲明

<?phpfunction arraysSum(array ...$arrays): array{return array_map(function(array $array): int {return array_sum($array);}, $arrays);}print_r(arraysSum([1,2,3], [4,5,6], [7,8,9])); 

3.??運算子

?? 用於替代需要isset的場合,這是一個文法糖。

<?php// Fetches the value of $_GET['user'] and returns 'nobody'// if it does not exist.$username = $_GET['user'] ?? 'nobody';// This is equivalent to:$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';// Coalescing can be chained: this will return the first// defined value out of $_GET['user'], $_POST['user'], and// 'nobody'.$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'; 

4.<=> 比較子

就是看兩個運算式值的大小,三種關係: = 返回0、< 返回-1、 > 返回 1

<?php// Integersecho 1 <=> 1; // 0echo 1 <=> 2; // -1echo 2 <=> 1; // 1// Floatsecho 1.5 <=> 1.5; // 0echo 1.5 <=> 2.5; // -1echo 2.5 <=> 1.5; // 1// Stringsecho "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1 

5.define支援定義數群組類型的值

php 5.6已經支援CONST 文法定義數組類的常量,PHP7中支援define文法。

<?phpdefine('ANIMALS', ['dog','cat','bird']);echo ANIMALS[1]; // outputs "cat" 

6.匿名類

<?phpinterface Logger {public function log(string $msg);}class Application {private $logger;public function getLogger(): Logger {return $this->logger;}public function setLogger(Logger $logger) {$this->logger = $logger;}}$app = new Application;$app->setLogger(new class implements Logger {public function log(string $msg) {echo $msg;}});var_dump($app->getLogger()); 

7.增加了整除函數 intdiv

小結:

PHP 7在效能方面的突破成為近來最熱門的話題之一,目前官方PHP 7.0.0 Beta 2已經發布

新特性

效能提升:PHP 7要比PHP 5.6快兩倍

全面一致的64位支援

移除了一些老的不在支援的SAPI(伺服器端應用編程連接埠)和擴充

新增了空接合操作符(??)

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.