Loading...
Loading...
00:00:00

PHP Math Built-in functions

PHP provides a wide range of built-in math functions that allow you to perform various mathematical operations. Here are some of the most commonly used math functions in PHP, along with examples of how to use them:

  1. abs(): Returns the absolute value of a number.
    echo abs(-4.2); // 4.2
    
  2. sqrt(): Returns the square root of a number.
    echo sqrt(16); // 4
    
  3. pow(): Returns the value of a number raised to a specified power.
    echo pow(2, 3); // 8
    
  4. exp(): Returns the exponential value of a number.
    echo exp(1); // 2.718281828459
    
  5. log(): Returns the natural logarithm of a number.
    echo log(2.718281828459); // 1
    
  6. floor(): Returns the next lowest integer value.
    echo floor(4.9); // 4
    
  7. ceil(): Returns the next highest integer value.
    echo ceil(4.1); // 5
    
  8. round(): Returns the rounded value of a number.
    echo round(4.4); // 4
    echo round(4.6); // 5
    
  9. min(): Returns the lowest value in a set of numbers.
    echo min(1, 2, 3); // 1
    
  10. max(): Returns the highest value in a set of numbers.
    echo max(1, 2, 3); // 3
    
  11. rand(): Generates a random integer between two specified numbers.
    echo rand(1, 10); // 7
    

These are just a few examples of the many math functions available in PHP. By using these functions, you can perform a wide range of mathematical operations in your PHP code.

Here is the list of All PHP math functions

Sure, here is a list of all the built-in math functions in PHP along with a brief description of each:

  1. abs(): Returns the absolute value of a number.
  2. acos(): Returns the arc cosine of a number (in radians).
  3. acosh(): Returns the inverse hyperbolic cosine of a number.
  4. asin(): Returns the arc sine of a number (in radians).
  5. asinh(): Returns the inverse hyperbolic sine of a number.
  6. atan(): Returns the arc tangent of a number (in radians).
  7. atan2(): Returns the arc tangent of two variables.
  8. atanh(): Returns the inverse hyperbolic tangent of a number.
  9. base_convert(): Converts a number between different base systems.
  10. bindec(): Converts a binary number to a decimal number.
  11. ceil(): Rounds a number up to the nearest integer.
  12. cos(): Returns the cosine of a number (in radians).
  13. cosh(): Returns the hyperbolic cosine of a number.
  14. decbin(): Converts a decimal number to a binary number.
  15. dechex(): Converts a decimal number to a hexadecimal number.
  16. decoct(): Converts a decimal number to an octal number.
  17. deg2rad(): Converts degrees to radians.
  18. exp(): Returns the exponential value of a number.
  19. expm1(): Returns the exponential value of a number minus 1.
  20. floor(): Rounds a number down to the nearest integer.
  21. fmod(): Returns the remainder of a division operation as a floating-point number.
  22. getrandmax(): Returns the maximum value that can be returned by the rand() function.
  23. hexdec(): Converts a hexadecimal number to a decimal number.
  24. hypot(): Returns the length of the hypotenuse of a right-angle triangle with sides of a given length.
  25. intdiv(): Performs integer division.
  26. is_finite(): Returns true if a value is finite.
  27. is_infinite(): Returns true if a value is infinite.
  28. is_nan(): Returns true if a value is not a number.
  29. lcg_value(): Generates a random number using the linear congruential method.
  30. log(): Returns the natural logarithm of a number.
  31. log10(): Returns the base-10 logarithm of a number.
  32. log1p(): Returns the natural logarithm of a number plus 1.
  33. max(): Returns the highest value in a set of numbers.
  34. min(): Returns the lowest value in a set of numbers.
  35. mt_getrandmax(): Returns the maximum value that can be returned by the mt_rand() function.
  36. mt_rand(): Generates a random number using the Mersenne Twister algorithm.
  37. octdec(): Converts an octal number to a decimal number.
  38. pi(): Returns the value of pi.
  39. pow(): Returns the value of a number raised to a specified power.
  40. rad2deg(): Converts radians to degrees.
  41. rand(): Generates a random integer between two specified numbers.
  42. round(): Rounds a number to the nearest integer.
  43. sin(): Returns the sine of a number (in radians).
  44. sinh(): Returns the hyperbolic sine of a number.
  45. sinh(), which returns the hyperbolic sine of a number
  1. sqrt(): Returns the square root of a number.
  2. srand(): Seeds the random number generator used by the rand() function.
  3. tan(): Returns the tangent of a number (in radians).
  4. tanh(): Returns the hyperbolic tangent of a number.
  5. trunc(): Truncates a floating-point number to an integer.

Note that these are just brief descriptions of each function, and there are often many parameters and options that can be passed to these functions to modify their behavior. It's important to consult the PHP documentation for each function to get a full understanding of how to use it properly.