PHP7语法新特性
echo 1 <=> 2;
echo 1 <=> 1;
echo 2 <=> 1;
delcare(strict_types=1);
function test_declare(int $a) : int
{
return $a * 2;
}
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$page = $_GET['page'] ?? 0;
define('ANIMALS', ['dog', 'cat', 'bird']);
use Space\{Class1, Class2, Class3};
try {
testFunc();
} catch (Erro $e) {
var_dump($e);
}
----------------------------------------------
15:04 debian@debian:blog $vim test.php
set_exception_handler(
function($e) {
var_dump($e;
});
testFunc();
var_dump(111);
15:04 debian@debian:blog $php test.php
object(Error)
["message":protected]=>
string(37) "Call to undefined function testFunc()"
["string":"Error":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(32) "/home/wwwroot/default/blog/test.php"
["line":protected]=>
int(17)
["trace":"Error":private]=>
array(0) {
}
["previous":"Error":private]=>
NULL
}
1 <?php
2
3 class Test
4 {
5 private $num = 1;
6 }
7
8 $f = function() {
9 return $this->num + 1;
10 };
11
12 echo $f->call(new Test) . "\n";
13
14
15 ?>
1 <?php
2
3 echo (15 / 4) . "\n";
4 echo (int)(15 / 4) . "\n";
5 echo intdiv(15, 4) . "\n";
6
7 ?>
15:12 debian@debian:default $php test.php
3.75
3
3
$arr = [1, 2, 3];
list($a, $b, $c) = $arr;
$arr = [1, 2, 3];
[$a, $b, $c] = $arr;
1 <?php
2
3 ($a)['b'] = 1;
4
5 var_dump($a);
6
7
8 ?>
15:28 debian@debian:default $php test.php
array(1) {
["b"]=>
int(1)
}