PHP5.5的新特性_PHP教程

來源:互聯網
上載者:User
看了@軒脈刃 今天出爐的PHP 5.5 新特性。不過沒有翻譯全,我這裡稍微補充下,整理成完整的一篇:)

原文:http://www.php.net/manual/zh/migration55.new-features.php

1,產生器

php5.5通過引入yield關鍵字來支援generators。Generators提供了一種更加簡潔的產生迭代器的方式,無需通過笨重複雜的Iterator介面。

一個簡單的樣本:利用generators實現和range函數一樣的迭代功能

 xrange(, ,  = 1 ( = ;  <= ;  +=  'Single digit odd numbers: ' (xrange(1, 9, 2)   " " "\n";

以上常式會輸出:

Single digit odd numbers: 1 3 5 7 9

2,添加finally關鍵字

try-catch語句現在支援後邊添加finally。無論是否會拋出異常,finally中的代碼總是會被執行。

3,新的雜湊API

添加了一個新的hash函數API,它可以更簡單的產生安全的散列值,並且能夠像crypt()一樣用於密碼驗證管理。詳細請參閱:password_hash()。

4,foreach支援list()

現在,foreach能夠利用list()結構分解被嵌套的數組,並且提取其中的值。舉例來說:

 =1, 2],3, 4], (  (,  "A: ; B: \n"

以上常式會輸出:

A: 1; B: 2
A: 3; B: 4

更多文檔可以參見foreach手冊。

5,empty()支援運算式

現在可以將任意的運算式傳入empty()中。例如:

  ( "This will be printed.\n" (( "This will not be printed.\n"

以上常式會輸出:

This will be printed.

6,array和string字面的dereferencing

可以利用下標訪問array、string字面中的某個元素或者char。(ps:dereferencing不知道怎麼翻譯才準確...看了軒脈刃的表述:支援用下標擷取,呵呵,其實就是這個意思。)

 'Array dereferencing: ' [1, 2, 3][0 "\n" 'String dereferencing: ' 'PHP'[0 "\n";

以上常式會輸出:

Array dereferencing: 1
String dereferencing: P

7,通過::class擷取類名

可以利用ClassName::class 擷取class完整的類名。例如:

 ClassName:: "\n";

以上常式會輸出:

Name\Space\ClassName

8,新增擴充OPcache

Zend Optimiser+的opcode cache被加入了php擴充,成為OPcache extension。OPcache可以提高php的執行效能,它能夠在共用記憶體中儲存編譯好的opcode,以此節約原本為每個請求載入\解析php指令碼的時間。具體安裝和使用詳情請參考the installation instructions。

9,foreach支援非標量key

foreach現在支援任何類型的key。原生的php數組僅能夠用標量作為key,而現在可以通過實現Iterator::key()來返回任意類型的資料,並且foreach均能接收。

10,在win平台上支援Apache 2.4

windows上用於apache2.4的SAPI已被實現支援。

11,改進GD

GD擴充做出了很多改進,包括:

imageflip()函數

imagecrop() 和 imagecropauto() 函數

imagecreatefromwebp() 和 imagewebp() 函數

http://www.bkjia.com/PHPjc/440349.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/440349.htmlTechArticle看了@軒脈刃 今天出爐的PHP 5.5 新特性。不過沒有翻譯全,我這裡稍微補充下,整理成完整的一篇:) 原文:http://www.php.net/manual/zh/migration55....

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.