锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷模式: 强锟斤拷 (默锟斤拷) 锟斤拷 锟较革拷模式锟斤拷 锟斤拷锟节匡拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟酵诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷强锟斤拷模式锟斤拷锟斤拷锟较革拷模式锟斤拷锟斤拷 锟街凤拷锟斤拷(string), 锟斤拷锟斤拷 (int), 锟斤拷锟斤拷锟斤拷 (float), 锟皆硷拷锟斤拷锟斤拷值 (bool)锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷PHP5锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷停锟斤拷锟斤拷锟斤拷锟斤拷涌冢锟�锟斤拷锟斤拷锟斤拷 锟截碉拷锟斤拷锟斤拷锟斤拷
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
int(9)
要使锟斤拷锟较革拷模式锟斤拷一锟斤拷 declare 锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟侥讹拷锟斤拷锟斤拷锟斤拷锟斤拷味锟斤拷锟较革拷锟斤拷锟斤拷锟斤拷锟斤拷锟角伙拷锟斤拷锟侥硷拷锟斤拷锟斤拷摹锟� 锟斤拷锟街革拷畈伙拷锟接帮拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷也影锟届到锟斤拷锟斤拷锟侥凤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟轿硷拷 锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷, 锟斤拷锟矫碉拷PHP锟斤拷锟斤拷锟皆硷拷锟斤拷展锟叫硷拷锟截碉拷PHP锟斤拷锟斤拷锟斤拷
锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥碉拷锟斤拷示锟斤拷锟轿硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰节★拷
PHP 7 锟斤拷锟斤拷锟剿讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷支锟街★拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷指锟斤拷锟剿猴拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷锟酵★拷锟斤拷锟矫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锌锟斤拷玫锟斤拷锟斤拷锟斤拷锟酵拷锟�
<?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]));
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Array ( [0] => 6 [1] => 15 [2] => 24 )
锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥碉拷锟斤拷示锟斤拷锟缴参硷拷 锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷.
锟斤拷锟斤拷锟秸筹拷使锟斤拷锟叫达拷锟节达拷锟斤拷同时使锟斤拷锟斤拷元锟斤拷锟绞斤拷锟� isset()锟斤拷锟斤拷锟斤拷锟�
锟斤拷锟斤拷锟斤拷锟斤拷锟絥ull锟较诧拷锟斤拷锟斤拷锟� (??) 锟斤拷锟斤拷锓拷恰锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷为NULL
锟斤拷
锟斤拷锟酵会返锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷蚍祷锟斤拷锟斤拷牡诙锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
<?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';
?>
太锟秸达拷锟斤拷锟斤拷锟斤拷锟斤拷锟节比斤拷锟斤拷锟斤拷锟斤拷锟绞斤拷锟斤拷锟�$a小锟节★拷锟斤拷锟节伙拷锟斤拷锟�$b时锟斤拷锟街别返伙拷-1锟斤拷0锟斤拷1锟斤拷 锟饺较碉拷原锟斤拷锟斤拷锟斤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷冉瞎锟斤拷锟�锟斤拷锟叫的★拷
<?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
?>
Array 锟斤拷锟酵的筹拷锟斤拷锟斤拷锟节匡拷锟斤拷通锟斤拷 define() 锟斤拷锟斤拷锟藉。锟斤拷 PHP5.6 锟叫斤拷锟斤拷通锟斤拷 const 锟斤拷锟藉。
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // 锟斤拷锟� "cat"
?>
锟斤拷锟斤拷支锟斤拷通锟斤拷new class 锟斤拷实锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟洁,锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一些"锟矫后即凤拷"锟斤拷锟斤拷锟斤拷锟洁定锟藉。
<?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());
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
object(class@anonymous)#2 (0) { }
锟斤拷细锟侥碉拷锟斤拷锟皆参匡拷 锟斤拷锟斤拷锟斤拷.
锟斤拷锟斤拷锟揭伙拷锟斤拷锟�16锟斤拷锟斤拷锟斤拷式锟斤拷 Unicode codepoint锟斤拷锟斤拷锟斤拷印锟斤拷一锟斤拷双锟斤拷锟脚伙拷heredoc锟斤拷围锟斤拷 UTF-8 锟斤拷锟斤拷锟绞斤拷锟斤拷址锟斤拷锟斤拷锟� 锟斤拷锟皆斤拷锟斤拷锟轿猴拷锟斤拷效锟斤拷 codepoint锟斤拷锟斤拷锟揭匡拷头锟斤拷 0 锟角匡拷锟斤拷省锟皆的★拷
echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
a a (same as before but with optional leading 0's) 锟斤拷
Closure::call() 锟斤拷锟斤拷锟斤拷锟脚革拷锟矫碉拷锟斤拷锟杰o拷锟斤拷谈锟斤拷锟斤拷锟斤拷锟绞憋拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷媳瞻锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
<?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);
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
1 1
锟斤拷锟斤拷锟斤拷锟街硷拷锟斤拷峁╋拷锟斤拷锟饺拷姆锟绞斤拷锟斤拷锟斤拷锟缴匡拷锟斤拷锟斤拷锟捷★拷锟斤拷通锟斤拷锟斤拷锟斤拷锟斤拷锟侥凤拷式锟斤拷锟斤拷止潜锟节的达拷锟斤拷注锟诫。
<?php
// 锟斤拷锟斤拷锟叫的讹拷锟斤拷转锟斤拷为 __PHP_Incomplete_Class 锟斤拷锟斤拷
$data = unserialize($foo, ["allowed_classes" => false]);
// 锟斤拷锟斤拷 MyClass 锟斤拷 MyClass2 之锟斤拷锟斤拷锟斤拷卸锟斤拷锟阶拷锟轿� __PHP_Incomplete_Class 锟斤拷锟斤拷
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]);
// 默锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷械锟斤拷喽硷拷强山锟斤拷艿模锟斤拷锟酵拷锟绞★拷缘诙锟斤拷锟斤拷锟斤拷锟�
$data = unserialize($foo, ["allowed_classes" => true]);
锟斤拷锟斤拷锟接碉拷 IntlChar 锟斤拷旨锟节憋拷露锟斤拷锟斤拷锟斤拷锟� ICU 锟斤拷锟杰★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟洁静态锟斤拷锟斤拷锟斤拷锟节诧拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷 unicode 锟街凤拷锟斤拷
<?php
printf('%x', IntlChar::CODEPOINT_MAX);
echo IntlChar::charName('@');
var_dump(IntlChar::ispunct('!'));
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
10ffff COMMERCIAL AT bool(true)
锟斤拷要使锟矫达拷锟洁,锟斤拷锟饺帮拷装Intl锟斤拷展
预锟斤拷锟斤拷锟斤拷锟斤拷锟矫诧拷锟斤拷强之前锟斤拷 assert() 锟侥凤拷锟斤拷锟斤拷 锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫讹拷锟斤拷为锟斤拷杀锟斤拷锟斤拷锟斤拷锟斤拷峁╋拷锟斤拷锟斤拷锟绞э拷锟绞憋拷壮锟斤拷囟锟斤拷斐o拷锟斤拷锟斤拷锟斤拷锟�
锟较版本锟斤拷API锟斤拷锟节硷拷锟斤拷目锟侥斤拷锟斤拷锟斤拷锟斤拷维锟斤拷锟斤拷assert()锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟皆结构锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟� string锟斤拷一锟斤拷锟斤拷锟斤拷锟皆碉拷boolean锟斤拷
<?php
ini_set('assert.exception', 1);
class CustomError extends AssertionError {}
assert(false, new CustomError('Some error message'));
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
Fatal error: Uncaught CustomError: Some error message
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷缘锟斤拷锟斤拷锟剿碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节匡拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷assert()锟斤拷 expectations section锟铰斤拷锟揭碉拷锟斤拷
锟斤拷同一 namespace 锟斤拷锟斤拷锟斤拷唷拷锟斤拷锟斤拷统锟斤拷锟斤拷锟斤拷诳锟斤拷锟酵拷锟斤拷锟斤拷锟� use 锟斤拷锟� 一锟斤拷锟皆碉拷锟斤拷锟剿★拷
<?php
// 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 5.5 锟芥本锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷怨锟斤拷锟斤拷摹锟� 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷使锟斤拷 return 锟斤法锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟绞� 锟斤拷锟斤拷锟角诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷 锟斤拷锟斤拷通锟斤拷锟斤拷锟斤拷 Generator::getReturn() 锟斤拷锟斤拷锟斤拷锟斤拷取锟斤拷锟斤拷锟斤拷锟侥凤拷锟斤拷值锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟缴诧拷锟斤拷锟斤拷锟斤拷锟皆猴拷锟斤拷锟揭伙拷巍锟�
<?php
$gen = (function() {
yield 1;
yield 2;
return 3;
})();
foreach ($gen as $val) {
echo $val, PHP_EOL;
}
echo $gen->getReturn(), PHP_EOL;
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
1 2 3
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟杰癸拷锟斤拷锟斤拷锟斤拷锟秸碉拷值锟斤拷一锟斤拷锟角筹拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆o拷 锟斤拷为锟斤拷使锟矫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥客伙拷锟剿达拷锟斤拷锟斤拷锟街憋拷拥玫锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫拷锟斤拷悖╋拷姆锟斤拷锟街碉拷锟� 锟斤拷锟斤拷锟街帮拷姹撅拷锌突锟斤拷舜锟斤拷锟斤拷锟斤拷锟饺硷拷锟斤拷锟斤拷锟斤拷锟斤拷欠锟斤拷锟斤拷锟斤拷锟斤拷锟秸碉拷值然锟斤拷锟劫斤拷锟斤拷锟斤拷应锟斤拷锟斤拷 锟斤拷锟矫凤拷锟斤拷锟斤拷恕锟�
锟斤拷锟节o拷只锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟� yield from锟斤拷 锟酵匡拷锟皆帮拷一锟斤拷锟斤拷锟斤拷锟斤拷锟皆讹拷委锟缴革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 Traversable 锟斤拷锟斤拷锟斤拷锟� array锟斤拷
<?php
function gen()
{
yield 1;
yield 2;
yield from gen2();
}
function gen2()
{
yield 3;
yield 4;
}
foreach (gen() as $val)
{
echo $val, PHP_EOL;
}
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
1 2 3 4
锟铰加的猴拷锟斤拷 intdiv() 锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟侥筹拷锟斤拷锟斤拷锟姐。
<?php
var_dump(intdiv(10, 3));
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
int(3)
session_start() 锟斤拷锟皆斤拷锟斤拷一锟斤拷 array 锟斤拷为锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷 php.ini 锟侥硷拷锟斤拷锟斤拷锟矫碉拷 锟结话锟斤拷锟斤拷选锟斤拷锟斤拷
锟节碉拷锟斤拷 session_start() 锟斤拷时锟斤拷 锟斤拷锟斤拷锟窖★拷锟斤拷锟斤拷锟斤拷也支锟斤拷 session.lazy_write 锟斤拷为锟斤拷 默锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角打开的★拷锟斤拷锟斤拷锟斤拷锟斤拷锟角匡拷锟斤拷 PHP 只锟斤拷锟节会话锟叫碉拷锟斤拷锟捷凤拷锟斤拷锟戒化锟斤拷时锟斤拷锟� 写锟斤拷峄帮拷娲拷募锟斤拷锟斤拷锟斤拷锟结话锟叫碉拷锟斤拷锟斤拷没锟叫凤拷锟斤拷锟侥变,锟斤拷么 PHP 锟斤拷锟节讹拷取锟斤拷峄帮拷锟斤拷锟街拷锟� 锟斤拷锟斤拷锟截闭会话锟芥储锟侥硷拷锟斤拷锟斤拷锟斤拷锟轿猴拷锟睫改o拷锟斤拷锟斤拷通锟斤拷锟斤拷锟斤拷 read_and_close 锟斤拷实锟街★拷
锟斤拷锟界,锟斤拷锟叫达拷锟斤拷锟斤拷锟斤拷 session.cache_limiter 为 private锟斤拷锟斤拷锟斤拷锟节讹拷取锟斤拷匣峄帮拷锟斤拷锟街拷锟斤拷锟斤拷瞎乇栈峄帮拷娲拷募锟斤拷锟�
<?php
session_start([
'cache_limiter' => 'private',
'read_and_close' => true,
]);
?>
锟斤拷 PHP 7 之前锟斤拷锟斤拷使锟斤拷 preg_replace_callback() 锟斤拷锟斤拷锟斤拷时锟斤拷 锟斤拷锟斤拷锟斤拷锟矫匡拷锟斤拷锟斤拷锟斤拷锟绞斤拷锟揭达拷谢氐锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷艿锟斤拷鹿锟斤拷锟侥凤拷支锟斤拷锟诫。 锟斤拷使锟斤拷锟铰加碉拷 preg_replace_callback_array() 锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷使锟矫达拷锟斤拷锟斤拷蛹锟洁。
锟斤拷锟节o拷锟斤拷锟斤拷使锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷每锟斤拷锟斤拷锟斤拷锟斤拷式注锟斤拷氐锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷锟斤拷募锟斤拷锟� 锟斤拷锟斤拷应锟侥回碉拷锟斤拷锟斤拷锟斤拷锟角癸拷锟斤拷锟斤拷锟斤拷锟街碉拷锟�
锟铰硷拷锟斤拷锟斤拷锟斤拷锟斤拷平台锟侥猴拷锟斤拷锟斤拷 random_bytes() 锟斤拷 random_int() 锟斤拷锟斤拷锟斤拷锟斤拷锟竭帮拷全锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
锟斤拷之前锟芥本锟叫o拷list() 锟斤拷锟斤拷锟斤拷锟杰憋拷证 锟斤拷确锟斤拷展锟斤拷实锟斤拷锟斤拷 ArrayAccess 锟接口的讹拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷丫锟斤拷锟斤拷薷锟斤拷锟�