PHP Math Functions
PHP provides several math functions, including:
- abs(): Returns the absolute value of a number.
- ceil(): Returns the smallest integer greater than or equal to a number.
- floor(): Returns the largest integer less than or equal to a number.
- round(): Rounds a number to the nearest integer.
- sin(): Returns the sine of a number.
- cos(): Returns the cosine of a number.
- tan(): Returns the tangent of a number.
Example of PHP Math Functions
<?php
$number = 10.5;
echo abs($number) . "\n";
// Outputs: 10.5
echo ceil($number) . "\n";
// Outputs: 11
echo floor($number) . "\n";
// Outputs: 10
echo round($number) . "\n";
// Outputs: 11
?>
Output:
10.5
11
10
11
Explanation: This example demonstrates the use of several PHP math functions, including abs()
, ceil()
, floor()
, and round()
.
Single Line Example
<?php
echo abs(10.5) . "\n";
echo ceil(10.5) . "\n";
echo floor(10.5) . "\n";
echo round(10.5) . "\n";
echo sin(10.5) . "\n";
echo cos(10.5) . "\n";
echo tan(10.5) . "\n";
?>
Output:
10.5
11
10
11
-0.544021110889369
-0.839071529096515
0.64836082704607