Solutions for returning negative numbers from the remainder of a PHP integer

Source: Internet
Author: User

Although the PHP language is powerful, it does not mean that it has no disadvantages. During code writing, you may encounter some headaches. The following describes how to return a negative number from the remainder of a PHP integer.

Let's look at an example.

Copy codeThe Code is as follows:
$ Res = 16244799483;
Echo $ res %9999999;
// The output result is-5069794. The correct result is 4801107.

In fact, this is also a BUG in PHP. The most important thing is that PHP is a weak language. It has built-in machines to determine the user type.

But after all, the machine is a machine, and when an error is identified, it is like the above. So we need manual intervention.

So I want to use the following method to solve the problem of returning a negative number from the remainder of the PHP integer.

Copy codeThe Code is as follows:
$ Res = floatval (16244799483 );
Var_dump ($ res %9999999 );

We can see that the result is still incorrect-5069794.

However, it is worth noting that the return value is of the int type.

I thought about the problem of returning a negative number from the remainder of the PHP integer.

PHP returns an integer by default.

And when you define $ res = 16244799483;

In fact, it has already exceeded. Therefore, we need to add the forced type conversion to the float type.

However, this is not enough because % is an integer.

Therefore, we need a function fmod for the float type.

Therefore, the final solution for returning a negative number from the remainder of the PHP integer is:

Copy codeThe Code is as follows:
$ Res = floatval (16244799483 );
Var_dump (fmod ($ res, 9999999 ));

In this way, the problem of returning a negative number from the remainder of the PHP integer is solved .:)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.