Although the PHP language is powerful, but does not mean that it has no shortcomings, in the process of writing code will encounter some headache problems. Below we will introduce you to the solution of PHP integer return negative numbers.
Let's look at an example first.
Copy Code code as follows:
$res = 16244799483;
echo $res% 9999999;
The output is 5069794, and the correct result should be 4801107.
In fact, this is also a bug in PHP. The main thing is that PHP is a weakly typed language. He built a machine to judge the type of user.
But the machine is a machine. There are also times when judgment goes wrong. It's like the top. So we need human intervention.
So I thought about using the following method to solve the problem that the PHP integer remainder returns a negative number.
Copy Code code as follows:
$res = Floatval (16244799483);
Var_dump ($res% 9999999);
We see the result is still -5069794 wrong.
But it's worth noting that the return is an int type.
Think about it in detail. The problem with PHP integer return of negative numbers is this.
PHP takes Yume to think of integers.
And when one defines $res = 16244799483;
It has already overflowed. So add the force type conversion. becomes a float type.
But that's not enough. Because% this modulo calculation is still for integers.
So we need a function fmod. is for float type.
So ultimately the solution for the final PHP integer to return a negative number is:
Copy Code code as follows:
$res = Floatval (16244799483);
Var_dump (Fmod ($res, 9999999));
This solves the problem of the return of a negative number to the PHP integer fetch.:)