• php7 教程


    标量类型声明

    1. 分为强制模式和严格模式

    2. 这些类型的函数参数可以执行声明

    int, float, bool, string, interfaces, array, callable

    例如:

    function sum(int ...$ints){

        return array_sum($ints);

    }

    print(sum(2,'3',4.1)); //9

    如果在最头部加上代码: 

    declare(strict_types=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; // 改为: return $value+1.0;  则会报错

    }

    print(returnIntValue(5)); //5

    空合并运算符(??)

    $info = isset($_GET['name']) ? $_GET['name'] : 'not name';

    上面那种写法, 现在可以这么写:

    $info = $_GET[name'] ?? 'not name';

    $info = $_GET['name'] ?? $_POST['name'] ?? 'not name';

    飞船操作符

    用于比较两个表达式。当第一个表达式比第二个表达式分别小于,等于或大于它返回-1,0或1

    例如:

    print( 1 <=> 1);print("<br/>");

    print( 1 <=> 2);print("<br/>");

    print( 2 <=> 1);print("<br/>");

    返回:

    0

    -1

    1

    define常量数组

    在php5.6中只能用const定义, 现在可以用define:

    define('cats',['blue cat','white cat','black cat']);

    new class 匿名类

    $app->setLogger(new class{

        public function log(string $msg){

            print($msg);

        }

    });

    Closure::call()

    将一个闭包函数动态绑定到一个新的对象实例, 并执行该函数

    class A{

        private $x = 1;

    }

    $getX = function(){

        return $this->x;

    };

    echo $getX->call(new A);

    为unserialize()提供过滤

    IntlChar类

    随机数

    string random_bytes (int $length)     生成伪随机字节

    int random_int (int $min, int $max)   生成伪随机在整数

    use语句

    从同一个命名空间导入类, 函数, 常量, 而不需要多次使用use

    use comyiibai{ClassA, ClassB, ClassC as C};

    use function comyiibai{fn_a, fn_b, fn_c};

    use const comyiibai{ConstA, ConstB, ConstC};

    intdiv 函数

    执行整数除法并返回int

    $value = intdiv(10,3);

    print($value); //3

    session支持数组参数

    session_start([

       'cache_limiter' => 'private',

       'read_and_close' => true,

    ]);

    php7弃用的功能

    1. 类和方法的名称相同, 不行

    class A {

        function A(){

            ...

        }

    }

    2. 不能调用非静态方法

    class A {

        function b(){

            ...

        }

    }

    A::b(); //错!

  • 相关阅读:
    git 账号密码
    sql server 备份
    计算经纬度的两点之间的距离
    redis 安装
    webapi 可空参数
    Asp.Net MVC4 使用Unity 实现依赖注入
    sublime主题推荐
    分解质数因子
    如何在sublime+chrome中调试php代码?
    php的mysql语句里变量加不加单引号问题
  • 原文地址:https://www.cnblogs.com/yszr/p/10420631.html
Copyright © 2020-2023  润新知