Home > Backend Development > PHP Tutorial > New features of PHP5.5_PHP tutorial

New features of PHP5.5_PHP tutorial

WBOY
Release: 2016-07-20 11:13:33
Original
795 people have browsed it

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

1, generator

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";
Copy after login

The above routine will output:

Single digit odd numbers: 1 3 5 7 9

2, add finally keyword

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.

3, New Hash API

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().

4, foreach supports list()

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>
Copy after login

The above routine will output:

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

For more documentation, see the foreach manual.

5, empty() supports expressions

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>
Copy after login

The above routine will output:

This will be printed.

6. Dereferencing of array and string literals

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";
Copy after login

The above routine will output:

Array dereferencing: 1
String dereferencing: P

7, get the class name through ::class

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";
Copy after login

The above routine will output:

NameSpaceClassName

8, new extension OPcache

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.

9, foreach supports non-scalar key

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.

10, supports Apache 2.4 on win platform

SAPI for apache2.4 on windows has been implemented and supported.

11, improved GD

The GD extension has made many improvements, including:

imageflip()function

imagecrop() and imagecropauto() functions

imagecreatefromwebp() and imagewebp() function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440349.htmlTechArticleI saw the new features of PHP 5.5 released by @Xuanmaiyan today. However, there is no complete translation. I will add a little bit here and organize it into a complete article:) Original text: http://www.php.net/manual/zh/migration55....
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template