The `math` functions in PHP allow you to handle and manipulate numbers. PHP provides a plethora of math functions to work with integers, floating-point numbers, and more complex math operations. Let's dive into some of the most commonly used PHP math functions and their examples.
Addition, Subtraction, Multiplication, and Division:
<?php
$add = 5 + 3; // 8
$sub = 5 - 3; // 2
$mul = 5 * 3; // 15
$div = 5 / 3; // 1.6666666666667
?>
`round()`: Rounds a floating-point number.
<?php
echo round(3.4); // 3
echo round(3.5); // 4
?>
`ceil()`: Round fractions up.
<?php
echo ceil(3.4); // 4
?>
`floor()`: Round fractions down.
<?php
echo floor(3.6); // 3
?>
`rand()`: Generate a random integer.
<?php
echo rand(); // Any random number
echo rand(1, 10); // Random number between 1 and 10
?>
`mt_rand()`: A better random value generator than `rand()`.
<?php
echo mt_rand(); // Any random number
echo mt_rand(1, 50); // Random number between 1 and 50
?>
`max()`: Find the highest value.
<?php
echo max(1, 3, 5, 9, 7); // 9
?>
`min()`: Find the lowest value.
<?php
echo min(1, 3, 5, 9, 7); // 1
?>
`sin()`, `cos()`, and `tan()`:
<?php
echo sin(0); // 0
echo cos(0); // 1
echo tan(M_PI_4); // 1 (M_PI_4 is π/4)
?>
`pow()`: Exponential expression.
<?php
echo pow(2, 3); // 8 (2 raised to the power 3)
?>
`sqrt()`: Square root.
<?php
echo sqrt(9); // 3
?>
`log()`: Natural logarithm.
<?php
echo log(2.7183); // 1 (approximately, since 2.7183 is approximately e)
?>
`abs()`: Absolute value.
<?php
echo abs(-5); // 5
?>
`fmod()`: Returns the floating-point remainder (modulo) of the division of the arguments.
<?php
echo fmod(5.7, 1.3); // 0.5
?>
PHP also provides some built-in math constants:
<?php
echo M_PI; // Outputs: 3.1415926535898
?>
Remember that this is just an overview of PHP's math functions. PHP provides even more advanced mathematical and number-related functions that cater to various needs. Always consult the [official PHP documentation](https://www.php.net/manual/en/ref.math.php) for detailed information and the latest updates.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here