PHP 7 | chr () + strict_types === error?

I am trying to convert my working PHP 5.x code to a more modern PHP 7.x code base, so I added "declare (strict_types = 1);" in the first step, but it did not work properly.

code: ord(chr(ord("\xE9") / 64) | "\xC0");

demo: https://3v4l.org/680ts

github: https://github.com/voku/portable-utf8/blob/master/src/voku/helper/UTF8.php#L6613

PHP <7.0 or> 7.0 (without strict_types = 1) === 195

PHP> 7.0 (with string_types = 1) === 192

Maybe someone can explain this to me? I think this is due to "chr ()" expecting an integer, but do we get a float ?! But there is no warning or something like this ...? http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.strings.hex

+4
source share
1 answer

So let me break it.

ord("\xC0");

That 192. With or without strict types. Breakdown occurs here.

chr(ord("\xE9") / 64)

Now it ord("\xE9") / 64works up to 3.640625, and we can see where it breaks here

var_dump(ord(chr(3.640625)));

It is 0with strict types and 3without. Per manual forchr

string chr ( int $ascii )

, float, int. , , float int, 0

TypeError. , , .

. PHP,

var_dump(ord(chr('bob'))); // string 0, in all PHP versions
+2

Source: https://habr.com/ru/post/1670252/


All Articles