<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>New features</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="migration55.incompatible.html">Backward Incompatible Changes</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration55.deprecated.html">Deprecated features in PHP 5.5.x</a></div> <div class="up"><a href="migration55.html">Migrating from PHP 5.4.x to PHP 5.5.x</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="migration55.new-features" class="sect1"> <h2 class="title">New features</h2> <div class="sect2" id="migration55.new-features.generators"> <h3 class="title"><a href="language.generators.html" class="link">Generators</a> added</h3> <p class="para"> Support for <a href="language.generators.html" class="link">generators</a> has been added via the <strong class="command">yield</strong> keyword. Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the <a href="class.iterator.html" class="classname">Iterator</a> interface. </p> <p class="para"> A simple example that reimplements the <span class="function"><a href="function.range.html" class="function">range()</a></span> function as a generator (at least for positive <em>step</em> values): </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">xrange</span><span style="color: #007700">(</span><span style="color: #0000BB">$start</span><span style="color: #007700">, </span><span style="color: #0000BB">$limit</span><span style="color: #007700">, </span><span style="color: #0000BB">$step </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">$start</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">$limit</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">+= </span><span style="color: #0000BB">$step</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">yield $i</span><span style="color: #007700">;<br /> }<br />}<br /><br />echo </span><span style="color: #DD0000">'Single digit odd numbers: '</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*<br /> * Note that an array is never created or returned,<br /> * which saves memory.<br /> */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">xrange</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">9</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">) as </span><span style="color: #0000BB">$number</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$number</span><span style="color: #DD0000"> "</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> Single digit odd numbers: 1 3 5 7 9 </pre></div> </div> </div> </div> <div class="sect2" id="migration55.new-features.finally"> <h3 class="title"><a href="language.exceptions.html" class="link"><em>finally</em></a> keyword added</h3> <p class="para"> <em>try</em>-<em>catch</em> blocks now support a <a href="language.exceptions.html" class="link"><em>finally</em></a> block for code that should be run regardless of whether an exception has been thrown or not. </p> </div> <div class="sect2" id="migration55.new-features.password"> <h3 class="title">New password hashing API</h3> <p class="para"> A <a href="book.password.html" class="link">new password hashing API</a> that makes it easier to securely hash and manage passwords using the same underlying library as <span class="function"><a href="function.crypt.html" class="function">crypt()</a></span> in PHP has been added. See the documentation for <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span> for more detail. </p> </div> <div class="sect2" id="migration55.new-features.foreach-list"> <h3 class="title"><a href="control-structures.foreach.html" class="link"><em>foreach</em></a> now supports <span class="function"><a href="function.list.html" class="function">list()</a></span></h3> <p class="para"> The <a href="control-structures.foreach.html" class="link">foreach</a> control structure now supports unpacking nested arrays into separate variables via the <span class="function"><a href="function.list.html" class="function">list()</a></span> construct. For example: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array </span><span style="color: #007700">= [<br /> [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">],<br /> [</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">],<br />];<br /><br />foreach (</span><span style="color: #0000BB">$array </span><span style="color: #007700">as list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"A: </span><span style="color: #0000BB">$a</span><span style="color: #DD0000">; B: </span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> A: 1; B: 2 A: 3; B: 4 </pre></div> </div> </div> <p class="para"> Further documentation is available on the <a href="control-structures.foreach.html#control-structures.foreach.list" class="link">foreach</a> manual page. </p> </div> <div class="sect2" id="migration55.new-features.empty"> <h3 class="title"> <span class="function"><a href="function.empty.html" class="function">empty()</a></span> supports arbitrary expressions</h3> <p class="para"> Passing an arbitrary expression instead of a variable to <span class="function"><a href="function.empty.html" class="function">empty()</a></span> is now supported. For example: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">always_false</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}<br /><br />if (empty(</span><span style="color: #0000BB">always_false</span><span style="color: #007700">())) {<br /> echo </span><span style="color: #DD0000">"This will be printed.\n"</span><span style="color: #007700">;<br />}<br /><br />if (empty(</span><span style="color: #0000BB">true</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"This will not be printed.\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> This will be printed. </pre></div> </div> </div> </div> <div class="sect2" id="migration55.new-features.const-dereferencing"> <h3 class="title"><span class="type"><a href="language.types.array.html" class="type array">array</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span> literal dereferencing</h3> <p class="para"> <span class="type"><a href="language.types.array.html" class="type Array">Array</a></span> and <span class="type"><a href="language.types.string.html" class="type string">string</a></span> literals can now be dereferenced directly to access individual elements and characters: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Array dereferencing: '</span><span style="color: #007700">;<br />echo [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">'String dereferencing: '</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> Array dereferencing: 1 String dereferencing: P </pre></div> </div> </div> </p> </div> <div class="sect2" id="migration55.new-features.class-name"> <h3 class="title">Class name resolution via <a href="language.oop5.basic.html#language.oop5.basic.class.class" class="link">::class</a></h3> <p class="para"> It is possible to use <em>ClassName::class</em> to get a fully qualified name of class <em>ClassName</em>. For example: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Name</span><span style="color: #007700">\</span><span style="color: #0000BB">Space</span><span style="color: #007700">;<br />class </span><span style="color: #0000BB">ClassName </span><span style="color: #007700">{}<br /><br />echo </span><span style="color: #0000BB">ClassName</span><span style="color: #007700">::class;<br /><br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> Name\Space\ClassName </pre></div> </div> </div> </p> </div> <div class="sect2" id="migration55.new-features.opcache"> <h3 class="title"><a href="book.opcache.html" class="link">OPcache</a> extension added</h3> <p class="para"> The Zend Optimiser+ opcode cache has been added to PHP as the new <a href="book.opcache.html" class="link">OPcache extension</a>. OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. See <a href="opcache.installation.html" class="link">the installation instructions</a> for more detail on enabling and using OPcache. </p> </div> <div class="sect2" id="migration55.new-features.non-scalar-iterator-keys"> <h3 class="title"><a href="control-structures.foreach.html" class="link"><em>foreach</em></a> now supports non-scalar keys</h3> <p class="para"> <a href="control-structures.foreach.html" class="link">foreach</a> now supports keys of any type. While non-scalar keys cannot occur in native PHP arrays, it is possible for <span class="methodname"><a href="iterator.key.html" class="methodname">Iterator::key()</a></span> to return a value of any type, and this will now be handled correctly. </p> </div> <div class="sect2" id="migration55.new-features.windows-apache"> <h3 class="title">Apache 2.4 handler supported on Windows</h3> <p class="para"> The Apache 2.4 handler SAPI is now supported on Windows. </p> </div> <div class="sect2" id="migration55.new-features.gd"> <h3 class="title">Improvements to GD</h3> <p class="para"> Various improvements have been made to the GD extension, these include: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> Flipping support using the new <span class="function"><a href="function.imageflip.html" class="function">imageflip()</a></span> function. </span> </li> <li class="listitem"> <span class="simpara"> Advanced cropping support using the <span class="function"><a href="function.imagecrop.html" class="function">imagecrop()</a></span> & <span class="function"><a href="function.imagecropauto.html" class="function">imagecropauto()</a></span> functions. </span> </li> <li class="listitem"> <span class="simpara"> WebP read and write support using <span class="function"><a href="function.imagecreatefromwebp.html" class="function">imagecreatefromwebp()</a></span> & <span class="function"><a href="function.imagewebp.html" class="function">imagewebp()</a></span> respectively. </span> </li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="migration55.incompatible.html">Backward Incompatible Changes</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration55.deprecated.html">Deprecated features in PHP 5.5.x</a></div> <div class="up"><a href="migration55.html">Migrating from PHP 5.4.x to PHP 5.5.x</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>