Home >Backend Development >PHP7 >Regarding the processing of hexadecimal strings in PHP7

Regarding the processing of hexadecimal strings in PHP7

藏色散人
藏色散人forward
2021-11-19 17:07:211892browse

This article is from the PHP7 tutorial column to introduce to you the problems of "0xFFFFFFFF" and 0xFFFFFFFF in php7. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Specific question:

$t1 = 0x3FFFFFFF & (1 * (0xd5b42e11));
$t2 = 0x3FFFFFFF & (1 * ("0xd5b42e11"));
var_dump($t1,$t2);

The value of the above code in php7 (not included) and the following platforms is:

int(364129809)
int(364129809)

And the value in php7 is:

int(364129809)
int(0)

Excuse me, in the php7 environment, how should I deal with 0x.$str to make it the same as the above value?

Solution:

Starting from PHP7, strings containing hexadecimal are no longer considered numbers

If you have to check whether the string Containing hexadecimal numbers, the officially recommended code is

<?php
$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)
?>

which should be changed to

$t1 = 0x3FFFFFFF & (1 * (0xd5b42e11));
$t2 = 0x3FFFFFFF & (1 * (filter_var("0xd5b42e11", FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX)));
var_dump($t1,$t2);
for the above problems.

The above is the detailed content of Regarding the processing of hexadecimal strings in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete