PHP Number
PHP provides a number of built-in functions for performing various mathematical operations. Here are some examples:
// Absolute value
$result = abs(-10); // 10
// Round to the nearest integer
$result = round(3.7); // 4
// Round down to the nearest integer
$result = floor(3.7); // 3
// Round up to the nearest integer
$result = ceil(3.2); // 4
// Random integer between 1 and 10
$result = rand(1, 10); // e.g. 7
// Trigonometric functions (in radians)
$result = sin(0.5); // 0.479...
$result = cos(0.5); // 0.877...
$result = tan(0.5); // 0.546...
PHP also provides constants for commonly used mathematical values, such as M_PI for pi and M_E for the natural logarithm base e. Here are some examples:
php
// Circle area with radius 5
$area = M_PI * 5 * 5; // 78.539...
// Natural logarithm of 10
$result = log(10); // 2.302...
// Exponential function e^2
$result = exp(2); // 7.389...
In addition to these basic functions, PHP provides extensions for more advanced mathematical operations, such as statistics and linear algebra.
Tags:
PHP