Home >Backend Development >PHP Tutorial >New features of PHP5.5_PHP tutorial
Looking at the new features of PHP 5.5 released by @Xuanmaiyan today. But there is no complete translation, so I will add a little bit here and organize it into a complete article:)
Original text: http://www.php.net/manual/zh/migration55.new-features.php
php5.5 supports generators by introducing the yield keyword. Generators provide a more concise way to generate iterators without going through the cumbersome and complicated Iterator interface.
A simple example: using generators to achieve the same iteration function as the range function
<span function</span> xrange(<span $start</span>, <span $limit</span>, <span $step</span> = 1<span ) { </span><span for</span> (<span $i</span> = <span $start</span>; <span $i</span> <= <span $limit</span>; <span $i</span> += <span $step</span><span ) { yield </span><span $i</span><span ; } } </span><span echo</span> 'Single digit odd numbers: '<span ; </span><span /*</span><span * Note that an array is never created or returned, * which saves memory. </span><span */</span> <span foreach</span> (xrange(1, 9, 2) <span as</span> <span $number</span><span ) { </span><span echo</span> "<span $number</span> "<span ; } </span><span echo</span> "\n";
The above routine will output:
Single digit odd numbers: 1 3 5 7 9
The try-catch statement now supports adding finally at the end. Regardless of whether an exception is thrown, the code in finally will always be executed.
Added a new hash function API, which can more easily generate secure hash values and can be used for password verification management like crypt(). For details, please see: password_hash().
Now, foreach can use the list() structure to decompose the nested array and extract the values. For example:
<span $array</span> =<span [ [</span>1, 2],<span [</span>3, 4],<span ]; </span><span foreach</span> (<span $array</span> <span as</span> <span list</span>(<span $a</span>, <span $b</span><span )) { </span><span echo</span> "A: <span $a</span>; B: <span $b</span>\n"<span ; }</span>
The above routine will output:
A: 1; B: 2
A: 3; B: 4
For more documentation, see the foreach manual.
You can now pass arbitrary expressions into empty(). For example:
<span function</span><span always_false() { </span><span return</span> <span false</span><span ; } </span><span if</span> (<span empty</span><span (always_false())) { </span><span echo</span> "This will be printed.\n"<span ; } </span><span if</span> (<span empty</span>(<span true</span><span )) { </span><span echo</span> "This will not be printed.\n"<span ; }</span>
The above routine will output:
This will be printed.
You can use subscripts to access an element in array, string literal, or char. (ps: I don’t know how to translate dereferencing accurately... I read the description of Xuanmai Ren: it supports obtaining with subscripts, haha, that’s what it actually means.)
<span echo</span> 'Array dereferencing: '<span ; </span><span echo</span> [1, 2, 3][0<span ]; </span><span echo</span> "\n"<span ; </span><span echo</span> 'String dereferencing: '<span ; </span><span echo</span> 'PHP'[0<span ]; </span><span echo</span> "\n";
The above routine will output:
Array dereferencing: 1
String dereferencing: P
You can use ClassName::class to get the complete class name of class. For example:
<span namespace Name\Space; </span><span class</span><span ClassName {} </span><span echo</span> ClassName::<span class</span><span ; </span><span echo</span> "\n";
The above routine will output:
NameSpaceClassName
Zend Optimiser+’s opcode cache has been added to the php extension and becomes OPcache extension. OPcache can improve the execution performance of PHP. It can store compiled opcode in shared memory, thereby saving the time of loading and parsing PHP scripts for each request. Please refer to the installation instructions for specific installation and usage details.
foreach now supports any type of key. Native PHP arrays can only use scalars as keys, but now you can return any type of data by implementing Iterator::key(), and foreach can receive it.
SAPI for apache2.4 on windows has been implemented and supported.
The GD extension has made many improvements, including:
imageflip()function
imagecrop() and imagecropauto() functions
imagecreatefromwebp() and imagewebp() function