PHP观看手册后的--个人记录

二维数组循环 list (python好像也有类似的写法)
(PHP 5 >= 5.5.0, PHP 7)

$array = [
    [1,2],
    [3,4]
];

foreach ($array as list($a,$b)){
    echo $a .'--' . $b ."
"
; }

function 返回

function sum($a, $b): float {
    return $a + $b;
}

// Note that a float will be returned.
var_dump(sum(1, 2));

float(3)

调用方法

class Foo
{
    static function bar()
    {
        echo "bar\n";
    }
    function baz()
    {
        echo "baz\n";
    }
}

$func = array("Foo", "bar");
$func(); // prints "bar"
$func = array(new Foo, "baz");
$func(); // prints "baz"
$func = "Foo::bar";
$func(); // prints "bar" as of PHP 7.0.0; prior, it raised a fatal error

闭包

注意:Inherited variable’s value is from when the function is defined, not when called
( 继承变量的值来自函数的定义,而不是在调用时)

$message = 'hello';

$example = function () use ($message) {
    var_dump($message);
};

echo $example();

$message = 'world';
echo $example();

第二个输出还是hello

$message 父作用域中变量

$arg 函数传入变量

$example = function ($arg) use ($message) {
    var_dump($arg . ' ' . $message);
};
$example("hello");

你可能感兴趣的:(php,php)