techmore.in

PHP – Maths Functions

PHP provides a rich set of built-in mathematical functions that allow you to perform various calculations, manipulate numbers, and work with mathematical constants. Here's an overview of some commonly used math functions in PHP:

Basic Arithmetic Functions

abs()

Returns the absolute (positive) value of a number.

Example:

php
$number = -10; $absolute = abs($number); // $absolute is 10 ?>

sqrt()

Returns the square root of a number.

Example:

php
$number = 16; $sqrt = sqrt($number); // $sqrt is 4 ?>

pow()

Returns the result of raising a number to a power.

Example:

php
$base = 2; $exponent = 3; $result = pow($base, $exponent); // $result is 8 (2^3) ?>

exp()

Returns the exponential (e^x) of a number.

Example:

php
$number = 2; $exp = exp($number); // $exp is approximately 7.389 ?>

log()

Returns the natural logarithm (base e) of a number.

Example:

php
$number = 10; $log = log($number); // $log is approximately 2.302 ?>

log10()

Returns the base 10 logarithm of a number.

Example:

php
$number = 100; $log10 = log10($number); // $log10 is 2 ?>

Trigonometric Functions

PHP provides trigonometric functions that work with radians:

sin(), cos(), tan()

Compute the sine, cosine, and tangent of an angle, respectively.

Example:

php
$angle = 0.5; // in radians $sin = sin($angle); $cos = cos($angle); $tan = tan($angle); ?>

Rounding Functions

round()

Rounds a floating-point number to the nearest integer.

Example:

php
$number = 4.6; $rounded = round($number); // $rounded is 5 ?>

ceil()

Rounds a floating-point number up to the nearest integer.

Example:

php
$number = 4.3; $ceiled = ceil($number); // $ceiled is 5 ?>

floor()

Rounds a floating-point number down to the nearest integer.

Example:

php
$number = 4.9; $floored = floor($number); // $floored is 4 ?>

Random Number Functions

rand()

Generates a random integer.

Example:

php
$random = rand(1, 100); // Generates a random number between 1 and 100 ?>

mt_rand()

Generates a random integer using the Mersenne Twister algorithm, which is faster and produces better random numbers than rand().

Example:

php
$random = mt_rand(1, 100); // Generates a random number between 1 and 100 ?>

Constants

PHP provides several mathematical constants:

  • M_PI: Pi (π)
  • M_E: Euler's number (e)
  • M_SQRT2: Square root of 2
  • M_SQRT3: Square root of 3

Example:

php
$pi = M_PI; $euler = M_E; ?>

Mathematical Functions on Arrays

min(), max()

Returns the minimum or maximum value in an array, respectively.

Example:

php
$numbers = array(5, 2, 8, 1); $minValue = min($numbers); // $minValue is 1 $maxValue = max($numbers); // $maxValue is 8 ?>

Summary:

PHP's mathematical functions provide a robust set of tools for performing basic arithmetic, trigonometric calculations, rounding operations, random number generation, and working with mathematical constants. These functions are essential for various applications, from simple calculations to complex scientific computations. Understanding how to use these functions effectively can greatly enhance your PHP programming capabilities.