字符串处理变更
1. 含有十六进制数字的字符串不再被视为数字, 也不再区别对待:
var_dump("0x123" == "291"); // bool(false) (previously true)
var_dump(is_numeric("0x123")); // bool(false) (previously true)
var_dump("0xe" + "0x1"); // int(0) (previously 16)
var_dump(substr("foo", "0x1")); // string(3) "foo" (previously "oo")
// Notice: A non well formed numeric value encountered
可以使用filter_var()检测字符串是否包含十六进制字符或是否可以转成整形:
$str = "0xffff";
$int = filter_var($str, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX);
if (false === $int) {
throw new Exception("Invalid integer!");
}
var_dump($int); // int(65535)
2. 由于双引号字符串与 heredocs 语法中添加了Unicode Codepoint Escape语法, "\u{"如果跟随无效字符序列(非utf8-8字符), 将会触发fatal error
$str = "\u{xyz}"; // Fatal error: Invalid UTF-8 codepoint escape sequence
为了避免这种情况情况, 应当对前导反斜线转义:
$str = "\\u{xyz}"; // Works fine
"\u"无后继 { 则不受影响. 以下代码行为不变, 不会触发错误.
$str = "\u202e"; // Works fine
Relevant RFCs: