Home >Backend Development >PHP Tutorial >The main new features of PHP 7.0 that I compiled, the new features of php7.0_PHP tutorial
So far, PHP has officially released the RC5 version of php7, which is expected to be released around November Release the first official version! Now, the major features of php7 have definitely been finalized and there will be no more changes. The iterations of some subsequent versions are mainly bug fixes, optimizations and the like. Let’s talk about the new features of php7.0 that we have been looking forward to.
1. Scalar parameter type declaration
Now supports string, integer, float, and bool parameter declarations. Previously, only class names, interfaces, arrays, and Callables were supported
Two styles: forced conversion mode (default) and strict mode
<?php // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1));
2. Return type declaration
<?php function arraysSum(array ...$arrays): array { return array_map(function(array $array): int { return array_sum($array); }, $arrays); } print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
3.??Operator
?? is used to replace isset when isset is required. This is a syntactic sugar.
<?php // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // Coalescing can be chained: this will return the first // defined value out of $_GET['user'], $_POST['user'], and // 'nobody'. $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
4.96b4fef55684b9312718d5de63fb7121 Comparison operator
It depends on the size of the two expression values, three relationships: = returns 0, 795cc97063261a94132d69c42c80b50c returns 1
<?php // Integers echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1
5.define supports defining values of array type
php 5.6 already supports CONST syntax to define constants of array classes, and PHP7 supports define syntax.
<?php define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[1]; // outputs "cat"
6. Anonymous class
<?php interface Logger { public function log(string $msg); } class Application { private $logger; public function getLogger(): Logger { return $this->logger; } public function setLogger(Logger $logger) { $this->logger = $logger; } } $app = new Application; $app->setLogger(new class implements Logger { public function log(string $msg) { echo $msg; } }); var_dump($app->getLogger());
7. Added the integer division function intdiv
Summary:
PHP 7’s performance breakthrough has become one of the hottest topics recently, and the official PHP 7.0.0 Beta 2 has been released
New Features
Performance improvement: PHP 7 is twice as fast as PHP 5.6
Full and consistent 64-bit support
Removed some old SAPI (Server Side Application Programming Port) and extensions that are no longer supported
New null join operator (??)