search
Homephp教程php手册PHP7 new features introduction

We have compiled some commonly used new features, welcome to like! ! !

New operator
1. ??
$username = $_GET['user'] ?? '';
$username = isset($_GET['user']) ? $_GET['user'] : ' nobody';

2.
$number1 $number2; When $number1 is less than, equal to, or greater than $number2, return -1,0,1 respectively


New function
intdiv(divisor, divisor) — round the division result
intdiv(3, 2) //1



define can define arrays
define('ANIMALS', [
'dog',
'cat',
'bird'
]);


Return type declaration
function test() :int
{
return 1; //true
return '1'; //true
return 'string'; //false
}


Scalar type declaration
function test(string $name) :int
{
return 22;
}
string integer float boolean


Can catch core errors
Error hierarchy
Throwable
 Error
 ArithmeticError
  DivisionByZeroError

  AssertionError
  ParseError
  TypeError

Exception


Optimization of core sorting
php5: array(1=>0, 0=> 0) //Quick sort (unstable sort)
php7: array(0=>0, 1=>0) //Quick sort + selection sort (stable sort)



More abstract syntax than php5 Number (abstract snytax tree) AST
PHP -> Parser -> AST ->Opcodes -> Execution
Performance has increased, memory consumption has also increased but is negligible


Anonymous classes can be instantiated through new class An anonymous class
function getAnonymousClass($config) {
return new class($config) {};
}



Temporarily bind a method to the object and call
$f = function() {
p($ this->name);
};

class F {
private $name = 'F';
}

$f->call(new F);




Unified syntax variables
Brackets do not affect behavior from left to right

$a = 'b';
$b = ['1', '2 ', '3'];
var_dump($$a[1]);
var_dump(($$a)[1]);

php5:
Notice: Uninitialized string offset: 1 in E:Program FilesphpStudyWWWtest.php on line 4
Notice: Undefined variable: in E:Program FilesphpStudyWWWtest.php on line 4
NULL

Parse error: syntax error, unexpected '[' in E:Program FilesphpStudyWWWtest.php on line 4

php7: string(1 ) "2" string(1) "2"


Expression PHP5 PHP7 $$foo['bar']['baz'] ${$foo['bar']['baz']} ($$foo )['bar']['baz']
$foo->$bar['baz']   $foo->{$bar['baz']}   ($foo->$bar)['baz ']
$foo->$bar['baz']()   $foo->{$bar['baz']}()  ($foo->$bar)['baz']()
Foo::$bar['baz']()   Foo::{$bar['baz']}()   (Foo::$bar)['baz']()




About namespaces
/ / Pre PHP 7 code
use somenamespaceClassA;
use somenamespaceClassB;
use somenamespaceClassC as C;

use function somenamespacefn_a;

use function somenamespacefn_b;
use function somenamespacefn_c;

use const somenamespaceConstA;

use const somenamespaceConstB;
use const somenamespaceConstC;

// PHP 7+ code

use somenamespace{ClassA, ClassB, ClassC as C};
use function somenamespace{fn_a, fn_b, fn_c};
use const somenamespace{ConstA, ConstB, ConstC};


modification of list
1.
list($array[], $array[], $array[]) = [1, 2, 3];
var_dump($array);
php5: array(3) { [0]=> ; int(3) [1]=> int(2) [2]=> int(1) }
  php7: array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }


2. Assigning null value is not allowed
list() = $a;
list(,,) = $a;
list($x, list (), $y) = $a;

php7 will report Fatal error: Cannot use empty list

3. String splitting is no longer supported
$string = "xy";
list($x, $y) = $string;
var_dump($x, $y);

php5: string(1) "x" string(1) "y"
php7: null null

Modification of foreach
Variable reference will affect the array Loop of
$array = [0];
foreach ($array as &$val) {
var_dump($val);
$array[1] = 1;
}
php5: int(0)
php7: int (0) int(1)

Reference source address: http://www.php7.site/book/php7/variable-changes-22.html

Tips to improve PHP performance: http://www.laruence.com/2015/12/04/3086.html

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor