How to convert negative numbers to positive integers by php
This article mainly explains "how php turns negative numbers into positive integers". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how php turns negative numbers into positive integers".
Php turns negative numbers into positive integers: 1, use abs () function to turn negative numbers into positive numbers, use intval () function to round positive numbers and turn them into positive integers, syntax "intval (abs ($number))"; 2, use the "~" bit operator to add negative numbers to one, syntax "~ $number + 1".
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The method of changing negative numbers into positive integers by php
Method 1: use the abs () and intval () functions
The abs () function turns a negative number into a positive number.
The intval () function is rounded to a positive integer.
Implementation code:
Method 2: use the "~" bit operator to reverse the negative number by one.
Description:
After the positive number is inverted by one, the complement of the negative number is obtained, and the negative number is stored in memory in the form of a complement, and the conversion of the complement to the original code is the corresponding negative number after the positive number is transformed.
When a negative number is inverted by one, you get a complement, but the original complement of a positive number is the same.
Note: inversion and inversion are two different concepts, and both take part in the operation in the form of complement, because negative numbers participate in the operation.
At this point, I believe you have a deeper understanding of "how php turns negative numbers into positive integers". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!