我有以下代码在不同的PHP版本中产生不同的结果。我是否缺少某些东西,或者它是PHP 7.1.24中的错误,还是有可能会影响此的PHP设置?
<?php
$a = "\x43\xbe\xb7\xe8\x30\x00\x10\x00\x00\x00";
$b = "\x4b\x40";
$c = $a . $b;
echo strlen($a) . "<br>";
echo strToHex($a) . "<br>";
echo strlen($b) . "<br>";
echo strToHex($b) . "<br>";
echo strlen($c) . "<br>";
echo strToHex($c) . "<br>";
function strToHex($string) {
$hex = '';
for ($i = 0; $i < strlen($string); $i++) {
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
PHP 7.1.24输出:
8
43beb7e8300100
2
4b40
10
43beb7e830010000
所需结果: PHP 5.6.40,PHP 7.1.27,PHP 7.2.16、7.3.3
10
43beb7e830010000
2
4b40
12
43beb7e8300100004b40
示例2
<?php
$a = "\xe8\xf1\xf2\xf3\xf4\xf5";
echo strlen($a) . "<br>";
echo strToHex($a) . "<br>";
$b = array(
"\xe8",
"\xf1",
"\xf2",
"\xf3",
"\xf4",
"\xf5"
);
$b = implode("", $b);
echo strlen($b) . "<br>";
echo strToHex($b) . "<br>";
function strToHex($string) {
$hex = '';
for ($i = 0; $i < strlen($string); $i++) {
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
错误的结果:
2
e8f1
2
e8f1
好的结果:
6
e8f1f2f3f4f5
6
e8f1f2f3f4f5