Heim  >  Artikel  >  php教程  >  我整理的PHP 7.0主要新特性,php7.0新特性

我整理的PHP 7.0主要新特性,php7.0新特性

WBOY
WBOYOriginal
2016-06-13 08:48:01906Durchsuche

我整理的PHP 7.0主要新特性,php7.0新特性

截止到目前为止,PHP官方已经发布了php7的RC5版本,预计在11月份左右会发布第一个正式版本!现在来说php7的重大特性肯定已经是定型了,不会再有什么变动了。后续一些版本的迭代主要也就是修修bug,优化之类的。下面就来说话我们一直期待的php7.0新特征吧。

1.标量参数类型声明

现在支持字符串(string)、整型(int)、浮点数(float)、及布尔型(bool)参数声明,以前只支持类名、接口、数组及Callable
两种风格:强制转换模式(默认)与严格模式

<&#63;php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1)); 

2.返回类型声明

<&#63;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.??运算符

?? 用于替代需要isset的场合,这是一个语法糖。

<&#63;php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] &#63;&#63; 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) &#63; $_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'] &#63;&#63; $_POST['user'] &#63;&#63; 'nobody'; 

4. 比较运算符

就是看两个表达式值的大小,三种关系: = 返回0、 返回 1

<&#63;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支持定义数组类型的值

php 5.6已经支持CONST 语法定义数组类的常量,PHP7中支持define语法。

<&#63;php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat" 

6.匿名类

<&#63;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.增加了整除函数 intdiv

小结:

PHP 7在性能方面的突破成为近来最热门的话题之一,目前官方PHP 7.0.0 Beta 2已经发布

新特性

性能提升:PHP 7要比PHP 5.6快两倍

全面一致的64位支持

移除了一些老的不在支持的SAPI(服务器端应用编程端口)和扩展

新增了空接合操作符(??)

您可能感兴趣的文章:

  • Win2008 R2 IIS7 PHP 5.4 环境搭建图文教程
  • win2008 R2 下 IIS7.5+PHP5.2.17+Mysql5.5.16+Zend3.3.3
  • win7计划任务定时执行PHP脚本设置图解
  • windows7下安装php的imagick和imagemagick扩展教程
  • windows7下安装php的php-ssh2扩展教程
  • PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法
  • IIS7,IIS7.5 升级PHP5.3后站点变慢的解决方法
  • IIS7配置PHP图解(IIS7+PHP_5.2.17/PHP_5.3.5)
  • win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
  • php中隐形字符65279(utf-8的BOM头)问题
  • Win7 64位系统下PHP连接Oracle数据库
  • 深入浅析PHP7.0新特征(五大新特征)
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn