之前版本中,number_format()
有可能會返回 -0
。雖然這是符合 IEEE 754
規(guī)范的,但是這樣會導(dǎo)致可讀性不好,新版本中會將這樣的負(fù)數(shù)去掉。
<?php
var_dump(number_format(-0.01)); // 新版本輸出 string(1) "0" 舊版本輸出 string(2) "-0"
將數(shù)組轉(zhuǎn)換為對象,或?qū)ο筠D(zhuǎn)換為數(shù)組時(shí),數(shù)字鍵現(xiàn)在得到了更好的處理(無論是通過顯式轉(zhuǎn)換還是通過 settype() 函數(shù))。
這意味著現(xiàn)在可以訪問數(shù)組中的整數(shù)(或者說是字符串整數(shù))鍵,這些鍵會映射到對象中:
<?php
// array to object
$arr = [0 => 1];
$obj = (object)$arr;
var_dump(
$obj,
$obj->{'0'}, // 新寫法
$obj->{0} // 新寫法
);
以上例程會輸出:
object(stdClass)#1 (1) { ["0"]=> // string key now, rather than integer key int(1) } int(1) int(1)
從對象轉(zhuǎn)換成的數(shù)組中的整數(shù)(或者說是字符串整數(shù))鍵現(xiàn)在也可以直接訪問:
<?php
// object to array
$obj = new class {
public function __construct()
{
$this->{0} = 1;
}
};
$arr = (array)$obj;
var_dump(
$arr,
$arr[0], // 新寫法
$arr['0'] // 新寫法
);
以上例程會輸出:
array(1) { [0]=> // integer key now, rather than string key int(1) } int(1) int(1)
null
參數(shù)
之前版本中,傳遞 null
給 get_class()
函數(shù)將返回當(dāng)前類名。在新版本中,此行為會拋出一個(gè)
E_WARNING
錯(cuò)誤。如果想實(shí)現(xiàn)與之前版本同樣的效果,請不要傳遞任何參數(shù)進(jìn)來。
對非可數(shù)類型調(diào)用 count()(或
sizeof())函數(shù),會拋出一個(gè)
E_WARNING
錯(cuò)誤。
<?php
var_dump(
count(null), // NULL is not countable
count(1), // integers are not countable
count('abc'), // strings are not countable
count(new stdclass), // objects not implementing the Countable interface are not countable
count([1,2]) // arrays are countable
);
以上例程會輸出:
Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d int(0) int(1) int(1) int(1) int(2)
As part of the long-term migration away from resources, the Hash extension has been updated to use objects instead of resources. The change should be seamless for PHP developers, except for where is_resource() checks have been made (which will need updating to is_object() instead).
下列默認(rèn)選項(xiàng)被修改:
tls://
默認(rèn)為 TLSv1.0 or TLSv1.1 or TLSv1.2
ssl://
成為 tls://
的別名
STREAM_CRYPTO_METHOD_TLS_*
常量默認(rèn)為 TLSv1.0
或 TLSv1.1 + TLSv1.2,替代之前的 TLSv1.0
之前版本中,如果在一個(gè)閉包資源中使用 gettype()
會返回字符串 "unknown type"
,現(xiàn)在將會返回字符
"resource (closed)"
。
之前版本中,對 __PHP_Incomplete_Class
調(diào)用 is_object() 函數(shù)會返回
false
,現(xiàn)在會返回 true
。
調(diào)用未定義的常量,現(xiàn)在會拋出一個(gè)
E_WARNING
錯(cuò)誤(之前版本中為 E_NOTICE
)。在下一個(gè)
PHP 大版本中,將會拋出一個(gè)
Error 錯(cuò)誤。
官方支持的最低 Windows 版本為 Windows 7/Server 2008 R2。
Compatibility checks upon default trait property values will no longer perform casting.
object
保留字的變化
object
在之前的 PHP 7.0 版本
中被聲明為軟保留字(soft-reserved)?,F(xiàn)在變更為強(qiáng)制保留字,禁止在任何類或接口中使用該名稱。
NetWare 已不再被支持。
SORT_STRING
While array_unique() with SORT_STRING
formerly copied the array and removed non-unique elements (without packing
the array afterwards), now a new array is built by adding the
unique elements. This can result in different numeric indexes.
The bcmod() function no longer truncates fractional
numbers to integers. As such, its behavior now follows
fmod(), rather than the %
operator.
For example bcmod('4', '3.5')
now returns
0.5
instead of 1
.
The hash_hmac(), hash_hmac_file(),
hash_pbkdf2(), and hash_init() (with
HASH_HMAC
) functions no longer accept non-cryptographic
hashes.
The json_decode() function option,
JSON_OBJECT_AS_ARRAY
, is now used if the second
parameter (assoc) is null
. Previously,
JSON_OBJECT_AS_ARRAY
was always ignored.
Sequences generated by rand() and mt_rand() for a specific seed may differ from PHP 7.1 on 64-bit machines (due to the fixing of a modulo bias bug in the implementation).
sql.safe_mode
ini 選項(xiàng)移除
sql.safe_mode
ini 設(shè)置項(xiàng)已被移除。
The zone
element of the array returned by date_parse() and
date_parse_from_format() represents seconds instead of
minutes now, and its sign is inverted. For instance -120
is now 7200
.
As of PHP 7.2.34, the names of incoming cookies are no longer url-decoded for security reasons.