zoukankan      html  css  js  c++  java
  • php7新内容总结(随时更新)

    一.参数和返回值类型申明

    可以申明的有:float,int,bool,string,interfaces,array,callable

    一般模式:
    function sum(int ...$ints) {
    return array_sum($ints);
    }
    print(sum(2, '3', 4.1)); //9
    严格模式:
    declare(strict_types=1);
    function sum(int ...$ints) {
    return array_sum($ints);
    }
    print(sum(2, '3', 4.1)); //Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, ...
      
    返回值:
    declare(strict_types = 1);
    function returnIntValue(int $value): int {
    return $value + 1.0;
    }
    print(returnIntValue(5));//Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned.

    二.空合并运算符
    isset($_GET['aa']) ? $_GET['aa'] : 'not passed'等价于$_GET['aa']??'not passed';

    三.飞船运算符
    print( 1 <=> 1);//0
    print( 1 <=> 2);//-1
    print( 2 <=> 1);//1

    四.定义数组常亮
    define('animals', [ 'a', 'b', 'c']);

    五.过滤unserialize
    PHP 7引入了过滤的unserialize()函数,以便在对不可信数据上的对象进行反序列化时提供更好的安全性。它可以防止可能的代码注入,并使开发人员能够对可以反序列化的类进行白名单。

    六.use 批量声明
    在同一个命名空间下,现在use可以批量申明
    use some/namespace/{ClassA, ClassB, ClassC as C};

    七.支持为负的字符串偏移量
    var_dump('abcdef'[-2]);
    var_dump(strpos("aabbcc", "b", -3));

    八.foreach不再改变内部数组指针
    $arr = [0,1,2];
    foreach($array as $val){
      var_dump(current($array));
    }
    php5 输出:int(1) int(2) bool(false)
    php7 输出:int(0) int(0) int(0)

  • 相关阅读:
    spring boot 集成activeMq
    spring boot配置跨域
    spring boot中使用mybatis逆向工程
    Cookie/Session/Token
    Spring Boot自定义Starter
    linux防火墙命令
    imx6ull+debian10 构建静态qt交叉编译环境
    Arm Qt编译Qt例程出错 GLES3/gl3.h: No such file or directory 解决方法
    QtCreator设置野火iMx6开发板提供的qt交叉编译套件
    联想ideapad-330C 在Ubuntu18.04 上安装Realtek 8821CE无线网卡驱动
  • 原文地址:https://www.cnblogs.com/crazytata/p/9318838.html
Copyright © 2011-2022 走看看