- 5 years ago
- Zaid Bin Khalid
- 13,346 Views
-
7
In this post, we will figure out how to perform numerical tasks in PHP. PHP also supports mathematical operations. So, in this tutorial, I will explain to you how you can perform these operations.
Performing Math Operations
The few math operations are shown in the below code. There are five basic arithmetic operators and in PHP you can use all of them.
<?php
echo 7 + 3; // 0utputs: 10
echo 7 - 2; // 0utputs: 5
echo 7 * 2; // 0utputs: 14
echo 7 / 2; // 0utputs: 3.5
echo 7 % 2; // 0utputs: 1
?>
It is just like math that you study in your school life. In math, DMAS rule applies whenever you try to perform division, multiplication, adding, and subtraction. In the DMAS the arithmetic operation work in this order Division, Multiplication, Addition, and Subtraction. The brackets change the scope of operation means the code between the breaks execute first then the rest of the calculation will work.
<?php
echo 5 + 4 * 10; // 0utputs: 45
echo (5 + 4) * 10; // 0utputs: 90
echo 5 + 4 * 10 / 2; // 0utputs: 25
echo 8 * 10 / 4 - 2; // 0utputs: 18
echo 8 * 10 / (4 - 2); // 0utputs: 40
echo 8 + 10 / 4 - 2; // 0utputs: 8.5
echo (8 + 10) / (4 - 2); // 0utputs: 9
?>
In the accompanying area, we’re going to see some implicit PHP capacities that are most habitually utilized in performing scientific activities.
Locate the Absolute Value of a Number
The abs() is return the integer value always but if you give float value then it will return float as shown in the below code.
<?php
echo abs(5); // 0utputs: 5 (integer)
echo abs(-5); // 0utputs: 5 (integer)
echo abs(4.2); // 0utputs: 4.2 (double/float)
echo abs(-4.2); // 0utputs: 4.2 (double/float)
?>
If the given number is negative then this function will return integer without negative number.
Cycle a Fractional Value Up or Down.
The ceil() and floor() both functions are used to round the value in PHP. The only difference is ceil() function round up the value and floor() function round down the value. Who to use both functions with example code is given below.
<?php
// Round fractions up
echo ceil(4.2); // 0utputs: 5
echo ceil(9.99); // 0utputs: 10
echo ceil(-5.18); // 0utputs: -5
// Round fractions down
echo floor(4.2); // 0utputs: 4
echo floor(9.99); // 0utputs: 9
echo floor(-5.18); // 0utputs: -6
?>
Square Root of a Number
PHP also provides the square root calculation for that you need to use the sqrt() function. A simple function that can return the square root value as shown in the below code. As you know negative number does not have square root value. In PHP negative value square root value will be NAN.
<?php
echo sqrt(9); // 0utputs: 3
echo sqrt(25); // 0utputs: 5
echo sqrt(10); // 0utputs: 3.1622776601684
echo sqrt(-16); // 0utputs: NAN
?>
Create a Random Number
The rand() function is simple and easy to understand. In PHP we can create a random number with the help of rand() function. There is also a function named uniqid() return the unique value with the prefix. The syntax is given below with an example.
rand ( int $min , int $max ) : int
The above code you can use to create random numbers.
<?php
// Generate some random numbers
echo rand() . "<br>";
echo rand() . "<br>";
// Generate some random numbers between 1 and 10 (inclusive)
echo rand(1, 10) . "<br>";
echo rand(1, 10) . "<br>";
//Unique ID function
echo uniqid('lwc-',true);
?>
There are few more operation in PHP you can use all those operations in your calculations. All other operations are listed below you can click on the name and see the details on the official site of PHP.
- decbin()
- bindec()
- dechex()
- hexdec()
and many more functions are available that you can use.
- 5 years ago
- Zaid Bin Khalid
- 13,346 Views
-
7