On the new characteristics of PHP7

Source: Internet
Author: User

The highest version of PHP I've ever used is php5.6. After a new job, the company is using PHP7. It is said that PHP7 's performance is much higher than before. The following is a new feature of PHP7. Strive for a simple understanding. Do not do in-depth research.

1. Variable type declaration
    • When the parameters of a function use strict mode, an exception is returned if the incoming parameter data type is inconsistent TypeError
    • The data types that can be declared are: Class/interface, self, array, callback, bool, float, int, string. [Reference-function parameters] (http://php.net/manual/zh/functions.arguments.php#functions.arguments.type-declaration)
2. Added a return value type declaration.
    • You can now specify the data type of the function's return value.
    • The return value declares the type, which refers to the variable type declaration.
function hasReturn() : bool{}
3.null Merge Operators
    • That's a good feature. Many ternary expressions +isset () can be written in this way.
    • Null merge operator: If the variable exists and the value is not NULL, it returns its own value, otherwise it returns its second operand. Much like MySQL's Ifnull ().
$value = $_GET[‘memc_codes‘] ?? ‘没使用优惠券啊‘;
4. Spaceship operator
    • Used to compare two expressions, to determine the size relationship between them, less than, equal to, or greater than the return -1,0,1 respectively.
echo 1 <=> 1;echo 1 <=> 2;echo 2 <=> 1;
5. Defin () can define array constants
    • In other words, constants can be defined as an array
6. An anonymous class can be instantiated with the new class
    • Anonymous class, you can throw it out.
7. Support for Unicode codepoint Translator 8. Closure::call () temporarily binds a method to an object closure and immediately calls
class A { private $lang = ‘php‘;}$getLang = function() { return $this->lang;};echo $getLang->call(new A); // 暂时将getLang函数绑定到A上,并执行它
9. Unserialize () The second parameter can filter the deserialized data
    • Prevent Code injection
10. Added Intlchar to manipulate Unicode character 11 for multi-character sets. ASSERT () increase, write codeception more convenient
    • ASSERT () is now a language structure. The first one can be an argument is an expression.
12. One use can import multiple classes
use model\A,B,C;
13. The generator can return an expression and get the value of the generator by calling the Generator::getreturn () method.
$gen = (function(){    yield 1;    return 3;})();foreach ($gen as $val) {    echo $val,PHP_EOL;}echo $gen->getReturn(),PHP_EOL; ## 这里
14. The yield from the generator can be used to introduce other generators
function gen() {    yield 1;    yield from gen2();}function gen2() {    yield 3;    yield 4;}froeach ($gen as $val) {    echo $val,PHP_EOL;}
15. New addition of normal function intdiv ()
echo intdiv(10,3); // 3
Session_Start () New parameter can modify system setting value
    • This deserves a closer look. Session options
17. Add Preg_replace_callback_array () function, you can pass multiple anonymous functions into the array. More concise than Preg_replace_callback (). 18. Add Random_bytes () and Random_int () to facilitate random number 19. You can use the list () function to expand an object that implements the Arrayaccess interface

On the new characteristics of PHP7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.