php7新特性的理解和比较

广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

php7新特性的理解和比较

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

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

//php7以前  if判断 if(empty($_GET['param'])) {      $param = 1; }else{     $param = $_GET['param']; }   //php7以前  三元运算符 $param = empty($_GET['param']) ? 1 : $_GET['param'];//PHP7  null合并运算符  $param = $_GET['param'] ?? 1;//1
登录后复制

2. define() 定义常量数组

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World
登录后复制

3. 组合比较符(<=>)

组合比较符用于比较两个表达式.当$a小于、等于或大于$b时它分别返回-1、0或1. 比较的原则是沿用PHP的常规比较规则进行的.

 /整数 echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1   //浮点数 echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1    //字符串echo "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1
登录后复制

4. 变量类型声明

两种模式: 强制(默认)和严格模式. 可以使用下列类型参数: string,int,float,bool

  //... 操作符: 表示这是一个可变参数. php5.6及以上的版本可使用: 函数定义的时候变量前使用.   function intSum(int ...$ints){      return array_sum($ints);   }  var_dump(intSum(2,'3.5'));//5      //严格模式   //模式声明:declare(strict_types=1);  默认情况值为0,值为1代表为严格校验的模式    declare(strict_types=1);  function add(int $a,int $b){      return $a+$b;  }  var_dump(add(2,'3.5')); //Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type integer
登录后复制

5. 返回值类型声明

增加返回类型声明的支持.类似于参数类型声明.(用法在函数定义的后面加 :类型名)

 //有效的返回类型declare(strict_types = 1); function getInt(int $value): int {   return $value; } print(getInt(6));//6
登录后复制

//无效返回类型declare(strict_types = 1); function getNoInt(int $value): int {   return $value+'2.5'; } print(getNoInt(6));//Fatal error: Uncaught TypeError: Return value of getNoInt() must be of the type integer
登录后复制

6. 匿名类

允许new class {} 创建一个匿名的对象.

  <?php   //php7以前 接口实现   interface User{       public function getDiscount();   }   class VipUser implements User{       //折扣系数       private $discount = 0.6;       public function getDiscount() {          return $this->discount;      }  }  class Goods{      private $price = 200;      private $objectVipUser;      //User接口VipUser类实现      public function getUserData($User){          $this->objectVipUser = $User;          $discount = $this->objectVipUser->getDiscount();          echo "商品价格:".$this->price*$discount;      }  }  $display = new Goods();  //常规实例化接口实现对象  $display ->getUserData(new VipUser);//商品价格:120
登录后复制

  <?php   //php7 创建一个匿名的对象   interface User{       public function getDiscount();   }   class Goods{       private $price = 200;       private $objectVipUser;       public function getUserData($User){          $this->objectVipUser = $User;          $discount = $this->objectVipUser->getDiscount();          echo "商品价格:".$this->price*$discount;      }  }  $display = new Goods();  //new匿名对象实现user接口  $display ->getUserData(new class implements User{      private $discount = 0.6;      public function getDiscount() {          return $this->discount;      }  });//商品价格:120
登录后复制

7. Closure::call()

Closure::call() 方法被添加为一个简短的方式来临时绑定一个对象作用域到一个闭包并调用它. 与PHP5的bindTo相比.它的性能要快得多.

  <?php   //php7以前   class A {       private  $attribute = 'hello world';   }      $getClosure = function(){       return $this->attribute;   };    $getAttribute = $getClosure->bindTo(new A, 'A');//中间层闭包  echo $getAttribute();//hello world
登录后复制

  <?php   //PHP7   class A {       private  $attribute = 'hello world';   }      $getClosure = function(){       return $this->attribute;   };    echo $getClosure->call(new A);//hello world
登录后复制

8. unserialize()

unserialize()函数:过滤的特性,可以防止非法数据进行代码注入,提供了更安全的反序列化数据

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World0
登录后复制

9. IntlChar

IntlChar:提供了一些可用于访问Unicode字符信息的实用方法的访问. 注意:必须安装Intl扩展才能使用!

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World1
登录后复制

10. CSPRNG

CSPRNG 函数提供一种简单的机制来生成密码的随机数.

random_bytes() -加密生存被保护的伪随机字符串.

random_int() -加密生存被保护的伪随机整数.

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World2
登录后复制

11. use 语句

可以使用单个use语句从相同的命名空间导入类,函数和常量,而不是使用多个use语句.

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World3
登录后复制

12. intp

新增加intp()函数,接收两个参数,返回值为第一个参数除于第二个参数的值并取整.

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World4
登录后复制

13. PHP7 错误处理

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World5
登录后复制

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World6
登录后复制

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World7
登录后复制

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World8
登录后复制

 //php7以前  define("CONTENT", "hello world");   echo CONTENT;//hello world    //PHP7  define('ANIMALS', [     'dog',      'cat',     'bird']); echo ANIMALS[2];//bird //PHP7 类外也可使用const来定义常量 const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World9
登录后复制

相关推荐:《PHP教程》

以上就是php7新特性的理解和比较的详细内容,更多请关注9543建站博客其它相关文章!

广告:SSL证书一年128.66元起,点击购买~~~

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。

作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:Vue中如何使用$forceUpdate强制更新组件
下一篇:laravel表单修改

发表评论

关闭广告
关闭广告