摘要: 标量类型声明 标量类型声明 有两种模式: 强制 (默认) 和 严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。
- <?php
- // Coercive mode
- function sumOfInts(int ...$ints)
- {
- return array_sum($ints);
- }
- var_dump(sumOfInts(2, '3', 4.1));
- <?php
- function arraysSum(array ...$arrays): array
- {
- return array_map(function(array $array): int {
- return array_sum($array);
- }, $arrays);
- }
- print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
- <?php
- // Fetches the value of $_GET['user'] and returns 'nobody'
- // if it does not exist.
- $username = $_GET['user'] ?? 'nobody';
- // This is equivalent to:
- $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
- // Coalesces can be chained: this will return the first
- // defined value out of $_GET['user'], $_POST['user'], and
- // 'nobody'.
- $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
- ?>
- <?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"; // 0
- echo "a" <=> "b"; // -1
- echo "b" <=> "a"; // 1
- ?>
- <?php
- define('ANIMALS', [
- 'dog',
- 'cat',
- 'bird'
- ]);
- echo ANIMALS[1]; // 输出 "cat"
- ?>
- <?php
- interface Logger {
- public function log(string $msg);
- }
- class Application {
- private $logger;
- public function getLogger(): Logger {
- return $this->logger;
- }
- public function setLogger(Logger $logger) {
- $this->logger = $logger;
- }
- }
- $app = new Application;
- $app->setLogger(new class implements Logger {
- public function log(string $msg) {
- echo $msg;
- }
- });
- var_dump($app->getLogger());
- ?>
- <?php
- class A {private $x = 1;}
- // PHP 7 之前版本的代码
- $getXCB = function() {return $this->x;};
- $getX = $getXCB->bindTo(new A, 'A'); // 中间层闭包
- echo $getX();
- // PHP 7+ 及更高版本的代码
- $getX = function() {return $this->x;};
- echo $getX->call(new A);
- <?php
- printf('%x', IntlChar::CODEPOINT_MAX);
- echo IntlChar::charName('@');
- var_dump(IntlChar::ispunct('!'));
- <?php
- ini_set('assert.exception', 1);
- class CustomError extends AssertionError {}
- assert(false, new CustomError('Some error message'));
- ?>
- // PHP 7 之前的代码
- use some\namespace\ClassA;
- use some\namespace\ClassB;
- use some\namespace\ClassC as C;
- use function some\namespace\fn_a;
- use function some\namespace\fn_b;
- use function some\namespace\fn_c;
- use const some\namespace\ConstA;
- use const some\namespace\ConstB;
- use const some\namespace\ConstC;
- // PHP 7+ 及更高版本的代码
- use some\namespace\{ClassA, ClassB, ClassC as C};
- use function some\namespace\{fn_a, fn_b, fn_c};
- use const some\namespace\{ConstA, ConstB, ConstC};
- ?>
- <?php
- $gen = (function() {
- yield 1;
- yield 2;
- return 3;
- })();
- foreach ($gen as $val) {
- echo $val, PHP_EOL;
- }
- echo $gen->getReturn(), PHP_EOL;
- <?php
- function gen()
- {
- yield 1;
- yield 2;
- yield from gen2();
- }
- function gen2()
- {
- yield 3;
- yield 4;
- }
- foreach (gen() as $val)
- {
- echo $val, PHP_EOL;
- }
- ?>
- <?php
- var_dump(intdiv(10, 3));
- ?>
- <?php
- session_start([
- 'cache_limiter' => 'private',
- 'read_and_close' => true,
- ]);
- ?>
- preg_replace_callback_array()
来源: https://yq.aliyun.com/articles/221530