当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP7.0新增功能详解(实例)

发布:smiling 来源: PHP粉丝网  添加日期:2020-02-05 13:23:46 浏览: 评论:0 

这一篇主要是来详细分析php7.0的新增功能。

一、性能与底层

PHP7速度是 PHP5.6 的两倍

php7 最显著的变化就是性能的极大提升,已接近Facebook开发的PHP执行引擎HHVM。在WordPress基准性能测试中,速度比5.6版本要快2~3倍,大大减少了内存占用。PHP7在语言上也有一些变化,比如添加返回类型声明、增加了一些新的保留关键字等。在安全方面,去除了PHP安全模式,添加魔术引号等。不仅如此,新版还支持64位,而且包含最新版Zend引擎。

测试一下

很简单的一个例子,生成一个 60 万元素的数组,通过查找key 的方式,来确定key是否存在。

  1. <?php 
  2.  
  3. $a = []; 
  4.  
  5. for($i=0;$i<600000;$i++){ 
  6.  
  7.   $a[$i] = $i
  8.  
  9.  
  10. foreach($a as $item) { 
  11.  
  12.  array_key_exists($item$a); 
  13.  

我们分别在php5.6.11和php7.0.4来测试下性能。

php5.6.11

  1. ➜ time php 1.php 
  2.  
  3.   0.67s user 0.06s system 67% cpu 1.078 total 
  4.  
  5. ➜ time php 1.php 
  6.  
  7.   0.68s user 0.06s system 98% cpu 0.748 total 
  8.  
  9. ➜ time php 1.php 
  10.  
  11.   0.65s user 0.06s system 67% cpu 1.052 total 

三次平均下来,大致是 user使用 0.65秒,system使用0.06秒,67%的cpu率。总共1秒左右。

再看php7的情况

  1. ➜  time /usr/local/opt/php70/bin/php 1.php 
  2.  
  3.   0.52s user 0.02s system 98% cpu 0.544 total 
  4.  
  5. ➜  time /usr/local/opt/php70/bin/php 1.php 
  6.  
  7.   0.49s user 0.02s system 99% cpu 0.513 total 
  8.  
  9. ➜  time /usr/local/opt/php70/bin/php 1.php 
  10.  
  11.   0.51s user 0.02s system 98% cpu 0.534 total 

对比下来,user使用时间下降20%左右,system使用时间下降70%,cpu使用率更高高达98%。总体时间减少为。0.5秒。

这个例子看下来,效率提供了2倍。确实不错。

再看一个例子。同样也是生成一个 60 万元素的数组,查找 value是否存在。

  1. <?php 
  2.  
  3. $a = []; 
  4.  
  5. for($i=0;$i<600000;$i++){ 
  6.  
  7.     $a[$i] = $i
  8.  
  9.  
  10. foreach($a as $i) { 
  11.  
  12.     array_search($i$a); 
  13.  
  14.  
  15. ?> 

先看php5.6.11

  1. ➜  testPHP time php 2.php 
  2.  
  3. 0.68s user 0.03s system 66% cpu 1.077 total 
  4.  
  5. ➜  testPHP time php 2.php 
  6.  
  7. 0.68s user 0.02s system 98% cpu 0.710 total 
  8.  
  9. ➜  testPHP time php 2.php 
  10.  
  11. 0.68s user 0.02s system 98% cpu 0.713 total 
  12.  
  13. ➜  testPHP time php 2.php 
  14.  
  15. 0.69s user 0.02s system 98% cpu 0.721 total 

再接着看php7.0.4

  1. ➜  testPHP time /usr/local/opt/php70/bin/php 2.php 
  2.  
  3. 0.12s user 0.02s system 69% cpu 0.201 total 
  4.  
  5. ➜  testPHP time /usr/local/opt/php70/bin/php 2.php 
  6.  
  7. 0.11s user 0.01s system 97% cpu 0.131 total 
  8.  
  9. ➜  testPHP time /usr/local/opt/php70/bin/php 2.php 
  10.  
  11. 0.11s user 0.01s system 96% cpu 0.130 total 

明显看出,快了6倍多。厉害。

二、新特性

1. 更多的标量类型声明

现在php的标量有两种模式: 强制 (默认) 和严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。它们扩充了PHP5中引入的其他类型:类名,接口,数组和 回调类型。在旧版中,函数的参数申明只能是(Array $arr)、(CLassName $obj)等,基本类型比如Int,String等是不能够被申明的。

怎么理解呢?php7之前的版本,我们要想限定一个函数的参数的类型,只有array或者class2种。

php7之前:

  1. class MyInfo 
  2.  
  3.  
  4.     public $a = 123; 
  5.  
  6.     public function getInfo(array $a$b
  7.  
  8.     { 
  9.  
  10.         var_dump($a$b); 
  11.  
  12.     } 
  13.  
  14.  
  15. function getClass(MyInfo $a) { 
  16.  
  17.     var_dump($a->a); 
  18.  

我们想限定 getInfo的第一个参数,必须是数组,所以,我们可以在参数$a前加一个array。来申明。

同样,我们想getClass的参数,必须是一个类,所以我们就用这个类的className前坠来申明,起到强制使用的目的。

php7之前,只有这2种标量可以使用。

我们来使用一下:

$info = new MyInfo();

$info->getInfo([1,2,3,4], 4);

我们按照规定的来,第一个参数,传数组,结果当然是正常打印:

  1. ➜  testPHP php 3.php 
  2.  
  3. array(3) { 
  4.  
  5.   [0] => 
  6.  
  7.   int(1) 
  8.  
  9.   [1] => 
  10.  
  11.   int(2) 
  12.  
  13.   [2] => 
  14.  
  15.   int(3) 
  16.  
  17.  
  18. int(4) 

要是我们不安装规定来,就会报知名错误:

$info = new MyInfo();

$info->getInfo(122, 0);

报错:

PHP Catchable fatal error:  Argument 1 passed to MyInfo::getInfo() must be of the type array, integer given, called in /Users/yangyi/www/testPHP/3.php on line 25 and defined in /Users/yangyi/www/testPHP/3.php on line 8

PHP Stack trace:

PHP   1. {main}() /Users/yangyi/www/testPHP/3.php:0

PHP   2. MyInfo->getInfo() /Users/yangyi/www/testPHP/3.php:25

使用类也一样:

$info = new MyInfo();

getClass($info);

输出结果:

➜  testPHP php 3.php

int(123)

同样,我们传入别的参数,就会报错:

  1. getClass(123); 
  2.  
  3. ➜  testPHP php 3.php 
  4.  
  5. PHP Catchable fatal error:  Argument 1 passed to getClass() must be an instance of MyInfo, integer given, called in /Users/yangyi/www/testPHP/3.php on line 27 and defined in /Users/yangyi/www/testPHP/3.php on line 17 
  6.  
  7. PHP Stack trace: 
  8.  
  9. PHP   1. {main}() /Users/yangyi/www/testPHP/3.php:0 
  10.  
  11. PHP   2. getClass() /Users/yangyi/www/testPHP/3.php:27 

我们回到这次php7的升级,它扩充了标量的类型,增加了bool、int、string、float。

php7有2种两种模式: 强制 (默认) 和严格模式。

强制模式

强制模式是默认模式,强制模式下,它会帮我们把数字类型的string类型,int整型,bool,强制类型。其他类型不能转换,就会报错。

还是刚才的例子:

  1. class MyInfo 
  2.  
  3.  
  4.     public $a = 123; 
  5.  
  6.     public function get1(bool $b
  7.  
  8.     { 
  9.  
  10.         var_dump($b); 
  11.  
  12.     } 
  13.  
  14.     public function get2(int $b
  15.  
  16.     { 
  17.  
  18.         var_dump($b); 
  19.  
  20.     } 
  21.  
  22.     public function get3(string $b
  23.  
  24.     { 
  25.  
  26.         var_dump($b); 
  27.  
  28.     } 
  29.  
  30.     public function get4(float $b
  31.  
  32.     { 
  33.  
  34.         var_dump($b); 
  35.  
  36.     } 
  37.  
  38.     public function get5(array $b
  39.  
  40.     { 
  41.  
  42.         var_dump($b); 
  43.  
  44.     } 
  45.  

我们先全部传入int 1

  1. $info = new MyInfo(); 
  2.  
  3. $info->get1(1); 
  4.  
  5. $info->get2(1); 
  6.  
  7. $info->get3(1); 
  8.  
  9. $info->get4(1); 

看打印结果,它已经帮我们强制转换了。

  1. ➜  testPHP /usr/local/opt/php70/bin/php 3.php 
  2.  
  3. /Users/yangyi/www/testPHP/3.php:11: 
  4.  
  5. bool(true) 
  6.  
  7. /Users/yangyi/www/testPHP/3.php:19: 
  8.  
  9. int(1) 
  10.  
  11. /Users/yangyi/www/testPHP/3.php:26: 
  12.  
  13. string(1) "1" 
  14.  
  15. /Users/yangyi/www/testPHP/3.php:33: 
  16.  
  17. double(1) 

我们继续,传入 string 1.23 :

  1. $info = new MyInfo(); 
  2.  
  3. $info->get1('1.23'); 
  4.  
  5. $info->get2('1.23'); 
  6.  
  7. $info->get3('1.23'); 
  8.  
  9. $info->get4('1.23'); 

看下,打印结果。也已经帮我们强制转换了。

  1. ➜  testPHP /usr/local/opt/php70/bin/php 3.php 
  2.  
  3. /Users/yangyi/www/testPHP/3.php:11: 
  4.  
  5. bool(true) 
  6.  
  7. /Users/yangyi/www/testPHP/3.php:19: 
  8.  
  9. int(1) 
  10.  
  11. /Users/yangyi/www/testPHP/3.php:26: 
  12.  
  13. string(4) "1.23" 
  14.  
  15. /Users/yangyi/www/testPHP/3.php:33: 
  16.  
  17. double(1.23) 

但是我们如果参数是array就没法强制转换,就会报错了:

  1. $info->get5('1.23'); 
  2.  
  3.  testPHP /usr/local/opt/php70/bin/php 3.php 
  4.  
  5. PHP Fatal error:  Uncaught TypeError: Argument 1 passed to MyInfo::get5() must be of the type array, string given, called in /Users/yangyi/www/testPHP/3.php on line 54 and defined in /Users/yangyi/www/testPHP/3.php:37 

我们在PHP5.6.11运行这些代码会报错吗?试一试:

  1. $info = new MyInfo(); 
  2.  
  3. $info->get1('1.23'); 
  4.  
  5. $info->get2('1.23'); 
  6.  
  7. $info->get3('1.23'); 
  8.  
  9. $info->get4('1.23'); 
  10.  
  11. ➜  testPHP php 3.php 
  12.  
  13. PHP Catchable fatal error:  Argument 1 passed to MyInfo::get1() must be an instance of bool, string given, called in /Users/yangyi/www/testPHP/3.php on line 48 and defined in /Users/yangyi/www/testPHP/3.php on line 8 

好吧。直接报错了,虽然错误提示也是说类型错误,但是,其他是不支持这些类型的申明。

严格模式

前面说了,强制模式下,它会帮我们强制转换,那么严格模式下呢?

首先,如何打开严格模式呢?

  1. <?php 
  2.  
  3. declare(strict_types=1); 

加上就可以了,这样,就进入严格模式,参数必须符合规定,不然报错:

我们加上这句话,再运行下:

  1. <?php 
  2.  
  3. declare(strict_types=1); 
  4.  
  5. ... 
  6.  
  7. ... 
  8.  
  9. $info = new MyInfo(); 
  10.  
  11. $info->get1('1.23'); 
  12.  
  13. $info->get2('1.23'); 
  14.  
  15. $info->get3('1.23'); 
  16.  
  17. $info->get4('1.23'); 

运行,看下结果,果然直接报错了。

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to MyInfo::get1() must be of the type boolean, string given, called in /Users/yangyi/www/testPHP/3.php on line 49 and defined in /Users/yangyi/www/testPHP/3.php:9

2. 返回值类型声明

我们知道php的函数是没有返回值类型的,return啥类型,就是啥类型。php7中增加了返回值类型,我们可以定义一个函数的返回值类型。

和php7升级的标量类型声明一样,return的类型可以是以下这些:bool、int、string、float、array、class。

举个例子来说,我们希望一个函数的返回值是一个数组,我们可以这样子书写:

:array {} // 冒号+返回类型

  1. function returnInfo ($a) : array { 
  2.  
  3.     return $a
  4.  
  5.  
  6. var_dump(returnInfo([1,2,3])); 

是不是觉得很奇怪,又无可思议!!!

查看打印结果:

  1. ➜  testPHP /usr/local/opt/php70/bin/php 3.php 
  2.  
  3. /Users/yangyi/www/testPHP/3.php:64: 
  4.  
  5. array(3) { 
  6.  
  7.   [0] => 
  8.  
  9.   int(1) 
  10.  
  11.   [1] => 
  12.  
  13.   int(2) 
  14.  
  15.   [2] => 
  16.  
  17.   int(3) 
  18.  

同样,我们想返回是int整型:

  1. function returnInfo ($a) : int { 
  2.  
  3.     return $a
  4.  
  5.  
  6. var_dump(returnInfo('1.233')); 

查看结果,他已经帮我们强制转换成整型了。

  1. ➜  testPHP /usr/local/opt/php70/bin/php 3.php 
  2.  
  3. /Users/yangyi/www/testPHP/3.php:64: 
  4.  
  5. int(1) 

同样,我们可以返回一个class类型的:

  1. public function getLogger(): Logger { 
  2.  
  3.     return $this->logger; 
  4.  

默认,也是强制模式,会帮我们转换,如果,我们想使用严格模式,同样是一样的,在文件头部加上:

  1. <?php 
  2.  
  3. declare(strict_types=1); 

就可以了,这样,我们规定返回值是什么类型,就必须得是这样,不然就报致命报错。

3. null合并运算符 (??)

由于日常使用中存在大量同时使用三元表达式和 isset()的情况, php7增加了一个新的语法糖 : null合并运算符 (??)

如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。

  1. //php version = 7  
  2.  
  3. $username = $user ?? 'nobody'
  4.  
  5. //php  version < 7 得这样使用: 
  6.  
  7. $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'

确实方便了很多。

我记得php5.3的更新中,加入了 三元运算符简写形式:

$a ?: $b

千万别和??搞混淆了!!!

$a ?: $b的意思是 $a为true时,直接返回$a, 否则返回$b

$a ?? $b的意思是 $a isset($a)为true, 且不为NULL, 就返回$a, 否则返回$b。

看例子:

  1. $user = 0; 
  2.  
  3. $username = $user ?? 'nobody'
  4.  
  5. echo $username;  //输出 0,因为 0 存在 且 不为NULL。 
  6.  
  7. $username = $user ?: 'nobody'
  8.  
  9. echo $username//输出 'nobody',因为 0 为 false 

4. 太空船操作符(组合比较符)

php7 中,新加入了一个比较符号:<=> ,因为长相像太空船,所以,也叫太空船操作符。

它有啥用呢?

<=>用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。

看例子:

  1. <?php 
  2.  
  3. // Integers 
  4.  
  5. echo 1 <=> 1; // 0 
  6.  
  7. echo 1 <=> 2; // -1 
  8.  
  9. echo 2 <=> 1; // 1 
  10.  
  11. // Floats 
  12.  
  13. echo 1.5 <=> 1.5; // 0 
  14.  
  15. echo 1.5 <=> 2.5; // -1 
  16.  
  17. echo 2.5 <=> 1.5; // 1 
  18.  
  19. // Strings 
  20.  
  21. echo "a" <=> "a"// 0 
  22.  
  23. echo "a" <=> "b"// -1 
  24.  
  25. echo "b" <=> "a"// 1 
  26.  
  27. ?> 

其实,蛮多地方可以派上用场的。

5. 通过define()定义常量数组

Array类型的常量现在可以通过 define()来定义。在 PHP5.6 中仅能通过const定义。

在php5.3中,增加了可以使用const来申明常量,替代define()函数,但是只能申明一些简单的变量。

  1. //旧式风格: 
  2.  
  3. define("XOOO""Value"); 
  4.  
  5. //新式风格: 
  6.  
  7. const XXOO = "Value"
  8.  
  9. //const 形式仅适用于常量,不适用于运行时才能求值的表达式: 
  10.  
  11. // 正确 
  12.  
  13. const XXOO = 1234; 
  14.  
  15. // 错误 
  16.  
  17. const XXOO = 2 * 617; 

在php5.6中,又对const进行来升级,可以支持上面的运算了。

const A = 2;

const B = A + 1;

但是,一只都是在优化const,可是确把define()给搞忘记了,php 5.6申明一个数组常量,只能用const。所以,在 php7 中把 define()申明一个数组也给加上去了。

  1. //php 7 
  2.  
  3. define ('AWS' , [12,33,44,55]); 
  4.  
  5. // php < 7 
  6.  
  7. const QWE = [12,33,44,55]; 
  8.  
  9. echo AWS[1]; //12 
  10.  
  11. echo QWE[2]; //33 

至此,到php7版本,define()的功能和const就一摸一样了,所以,你随便用哪一个都可以,但是因为在class类中,什么常量是const。所以,我们就统一用const申明常量好了。

6. 匿名类

现在已经支持通过new class 来实例化一个匿名类,这可以用来替代一些用后即焚的完整类定义。

看下这个官方文档上的一个栗子:

  1. <?php 
  2.  
  3. interface Logger { 
  4.  
  5.     public function log(string $msg); 
  6.  
  7.  
  8. class Application { 
  9.  
  10.     private $logger
  11.  
  12.     public function getLogger(): Logger { 
  13.  
  14.          return $this->logger; 
  15.  
  16.     } 
  17.  
  18.     public function setLogger(Logger $logger) { 
  19.  
  20.          $this->logger = $logger
  21.  
  22.     } 
  23.  
  24.  
  25. $app = new Application; 
  26.  
  27. $app->setLogger(new class implements Logger { 
  28.  
  29.     public function log(string $msg) { 
  30.  
  31.         echo $msg
  32.  
  33.     } 
  34.  
  35. }); 
  36.  
  37. var_dump($app->getLogger()); 
  38.  
  39. ?> 

我们先输出的打印的结果,显示为匿名类:

  1. class class@anonymous#2 (0) { 
  2.  

我们来分解下,还原被偷懒的少写的代码:

  1. class logClass implements Logger { 
  2.  
  3.     public function log(string $msg) { 
  4.  
  5.         echo $msg
  6.  
  7.     } 
  8.  
  9.  
  10. $app = new Application; 
  11.  
  12. $log2 = new logClass; 
  13.  
  14. $app->setLogger($log2); 

输出结果为:

class logClass#2 (0) {

}

虽然代码简洁了很多,但是还是有点不适应,多用用就好了。

还记得php中的匿名函数嘛?在php5.3中新增的匿名函数,结合新的,顺便复习下:

  1. function arraysSum(array ...$arrays): array { 
  2.  
  3.     return array_map(function(array $array): int { 
  4.  
  5.         return array_sum($array); 
  6.  
  7.     }, $arrays); 
  8.  
  9.  
  10. print_r(arraysSum([1,2,3], [4,5,6], [7,8,9])); 

输出结果为:

  1. Array 
  2.  
  3.  
  4.     [0] => 6 
  5.  
  6.     [1] => 15 
  7.  
  8.     [2] => 24 
  9.  

7. Unicode codepoint 转译语法

ps : 由于用的少,我就直接抄官网的说明了。

这接受一个以16进制形式的 Unicode codepoint,并打印出一个双引号或heredoc包围的 UTF-8 编码格式的字符串。 可以接受任何有效的 codepoint,并且开头的 0 是可以省略的。

  1. echo "\u{0000aa}"
  2.  
  3. echo "\u{aa}"//省略了开头的0 
  4.  
  5. echo "\u{9999}"

看下输出:

ª ª 香

我们在php5.6环境下执行下呢?会怎样:

\u{aa} \u{0000aa} \u{9999}

好吧,直接原样输出了。

8. Closure::call() 闭包

ps : 由于用的少,我就直接抄官网的说明了。

Closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。

  1. <?php 
  2.  
  3. class A {private $x = 1;} 
  4.  
  5. // php 7之前: 
  6.  
  7. $getXCB = function() {return $this->x;}; 
  8.  
  9. $getX = $getXCB->bindTo(new A, 'A'); // intermediate closure 
  10.  
  11. echo $getX(); 
  12.  
  13. // PHP 7: 
  14.  
  15. $getX = function() {return $this->x;}; 
  16.  
  17. echo $getX->call(new A); 

会输出:

1

1

9. 为unserialize()提供过滤

unserialize 这个函数应该不陌生,它是php中用解开用serialize序列化的变量。

看个例子:

  1. <?php 
  2.  
  3. $a = [1,2,3,4,5,6]; 
  4.  
  5. $b = serialize($a); 
  6.  
  7. $c = unserialize($b); 
  8.  
  9. var_dump($a$b$c); 

打印结果为:

  1. array(6) { 
  2.  
  3.   [0] => 
  4.  
  5.   int(1) 
  6.  
  7.   [1] => 
  8.  
  9.   int(2) 
  10.  
  11.   [2] => 
  12.  
  13.   int(3) 
  14.  
  15.   [3] => 
  16.  
  17.   int(4) 
  18.  
  19.   [4] => 
  20.  
  21.   int(5) 
  22.  
  23.   [5] => 
  24.  
  25.   int(6) 
  26.  
  27.  
  28. string(54) "a:6:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;}" 
  29.  
  30. array(6) { 
  31.  
  32.   [0] => 
  33.  
  34.   int(1) 
  35.  
  36.   [1] => 
  37.  
  38.   int(2) 
  39.  
  40.   [2] => 
  41.  
  42.   int(3) 
  43.  
  44.   [3] => 
  45.  
  46.   int(4) 
  47.  
  48.   [4] => 
  49.  
  50.   int(5) 
  51.  
  52.   [5] => 
  53.  
  54.   int(6) 
  55.  

现在php7中unserialize会变得更佳好用,它多了一个参数,用来反序列化包涵class的过滤不需要的类,变的更加安全。

  1. unserialize($one, ["allowed_classes" => true]); 
  2.  
  3.     unserialize($one, ["allowed_classes" => false]); 
  4.  
  5.     unserialize($one, ["allowed_classes" => [class1,class2,class3]]); 

举个例子,先序列化一个类。

  1. class MyInfo { 
  2.  
  3.         public function getMyName() 
  4.  
  5.         { 
  6.  
  7.                 return 'phper'
  8.  
  9.         } 
  10.  
  11.  
  12. $phper = new MyInfo(); 
  13.  
  14. $one = serialize($phper); 
  15.  
  16. //参数allowed_classes 设置为 true,表示允许解析class 
  17.  
  18. $two = unserialize($one, ["allowed_classes" => true]); 
  19.  
  20. //参数allowed_classes 设置为 false,表示不允许解析class 
  21.  
  22. $three = unserialize($one, ["allowed_classes" => false]); 
  23.  
  24. //不加参数。正常解析。 
  25.  
  26. $four = unserialize($one); 
  27.  
  28. //只允许解析 类 MyInfo1。 
  29.  
  30. $five = unserialize($one, ["allowed_classes" => ["MyInfo1"]]); 
  31.  
  32. //分别输出下 getMyName方法; 
  33.  
  34. var_dump($one); 
  35.  
  36. var_dump($two->getMyName()); 
  37.  
  38. var_dump($three->getMyName()); 
  39.  
  40. var_dump($four->getMyName()); 
  41.  
  42. var_dump($five->getMyName()); 

发现3和5直接报致命错误了:

PHP Fatal error:  main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "MyInfo" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition  in /Users/yangyi/www/php7/5.php on line 22

大致意思就是,没权限解析。

所以,我们改一下:

$three = unserialize($one, ["allowed_classes" => true]);

$five = unserialize($one, ["allowed_classes" => ["MyInfo"]]);

再输出,就正常了。

  1. /Users/yangyi/www/php7/5.php:22: 
  2.  
  3. string(17) "O:6:"MyInfo":0:{}" 
  4.  
  5. /Users/yangyi/www/php7/5.php:23: 
  6.  
  7. string(5) "phper" 
  8.  
  9. /Users/yangyi/www/php7/5.php:24: 
  10.  
  11. string(5) "phper" 
  12.  
  13. /Users/yangyi/www/php7/5.php:25: 
  14.  
  15. string(5) "phper" 
  16.  
  17. /Users/yangyi/www/php7/5.php:26: 
  18.  
  19. string(5) "phper" 

发现我目前为止并没用到,并没有什么乱用,好吧,继续下一个。

10. IntlChar

ps : 由于用的少,我就直接抄官网的说明了。

新增加的 IntlChar(http://php.net/manual/zh/class.intlchar.php) 类旨在暴露出更多的 ICU 功能。这个类自身定义了许多静态方法用于操作多字符集的 unicode 字符。

  1. <?php 
  2.  
  3. printf('%x', IntlChar::CODEPOINT_MAX); 
  4.  
  5. echo IntlChar::charName('@'); 
  6.  
  7. var_dump(IntlChar::ispunct('!')); 

以上例程会输出:

10ffff

COMMERCIAL AT

bool(true)

若要使用此类,请先安装Intl扩展.

Tags: PHP7 0新增功能

分享到: