NUMBERS IN PHP
In PHP, numbers are divided into integers and floats (floating point), we do not have to worry about how to represent a number, we simply assign it to a variable, then PHP will decide whether the number will be an integer or a float. PHP provides us with several functions to check numbers, such as whether a certain variable contains a number or not, like is_numeric(value).
In addition to the is_numeric(value ) function we have functions that check whether a type is an integer or a float, is_int(value) and is_float(value).
ARITHMETIC OPERATIONS
1)
+
ADDITION
2)
–
SUBTRACTION
3)
*
MULTIPLICATION
4)
/
DIVISION
5)
**
ELEVATION TO POWER
6)
%
MODULE OPERATOR
Since PHP is weakly typed, we can assign a number in the form of a string to a variable and add it to another number. What happens behind the scenes is the conversion of the string to an Integer.
COMPARISON OF FLOAT VALUES
When making comparisons on float values, we must be careful. Suppose we define a variable $x = 0.1 + 0.2 and a variable $y = 0.3. If we try to run the test $x == $y we will get a false. This depends on the standard of representation of floating-point numbers. The same problem occurs in javascript, which uses the same standard. We open the Chrome browser tools with F12.
As we can see 0.1 + 0.2 does not give exactly 0.3, this depends on the binary representation of floating points. To safely compare float numbers we need a maximum delta. If two float values have the smallest difference of this delta we can consider them equal.
In PHP we have many functions for numbers; the code below highlights some commonly used ones.
Leave A Comment