In today’s tutorial, we will learn about numbers in PHP. Numbers have numerous use in many programming languages. We use numbers in PHP too. There are two types of numbers used in PHP.
Integer includes all positive and negative numbers. Floats contain numbers that include decimal points.
In this part, we will discuss the integers. The integer numbers can perform different functionalities within a program. Some of the main uses are performing the mathematical operation like Division, Multiplication, Addition, Subtraction etc.
Below is a simple program in which we will learn how can we use numbers within PHP.
$a = 5;
$c= $a + $b;
The program will output the addition of 5 and 6 that is 11. All we do is just simply store the values of 5 and 6 in variables a and b. In PHP we declare a variable by putting the $ sign at the start. After declaring a and b as variables we declare another variable c and store the value of variable a, and b in this third variable. Now we can call the variable c to output the addition.
Below is another example that will further elaborate the use of numbers in PHP.
$a = 5;
echo “Result:”. ((( 2 + 3 + $a)* $b)/2 – 2);
The program will output 28.
First of all, PHP will solve the numbers by using basic mathematical rule, i.e. start solving the numbers nested within braces.
PHP also provides the different built-in function to perform different functionalities within PHP by using numbers. Below is the list of functions that are used to perform different functionalities within PHP program.
This function is used to calculate the absolute value.
It is also called exponential function. This function is used to calculate the exponent of any number. It takes two arguments, first argument is the number whose power we want to raise. For example in example of two power 6, we are raising the power of 2 i.e. two will be the first argument. And six will be the second argument i.e. it will be the power of 2. The output of the above function will be 64.
To calculate any random number. The output can be any number.
This function is used to calculate random numbers between 5 and 20. The output can be any number between 5 and 20.
Note that in above function we didn’t specify any argument to the function so it will take any random number. But in second one we specify two argument to function 5 and 20. Which will restrict the function between 5 and 20.
To calculate square root of a number. The output will be 5, as 5 is the square root of 25.
This function provide us the reminder. For example if we divide the 20 by 7 the remainder will be 6. This function will output the number 6.
Let me tell you here that 1 and “1” are not same. There is a lot of different between these two. 1 represent number and “1” represent string values. But PHP can convert the string into integers. After converting the string into integer PHP can also perform different mathematical functionalities on both values.
echo (1 + “2”);
echo (1 + “2 hours”);
//output will be 3 in both cases as PHP convert string into integer when perform mathematical functionality of integer with string.
You can find more information about integers in php by clicking here.