PHP Number Functions


PHP provides a wide range of number-related functions for performing calculations, formatting numbers, and handling mathematical operations. Here’s a list of important number functions in PHP:

Mathematical Functions:

  1. abs() - Returns the absolute value of a number.

    • Example: abs(-4.5) returns 4.5.
  2. ceil() - Rounds a number up to the nearest integer.

    • Example: ceil(4.3) returns 5.
  3. floor() - Rounds a number down to the nearest integer.

    • Example: floor(4.7) returns 4.
  4. round() - Rounds a floating-point number to a specified precision.

    • Example: round(4.567, 2) returns 4.57.
  5. sqrt() - Returns the square root of a number.

    • Example: sqrt(16) returns 4.
  6. pow() - Raises a number to the power of another.

    • Example: pow(2, 3) returns 8.
  7. max() - Returns the highest value in an array or a list of numbers.

    • Example: max(2, 3, 9, 4) returns 9.
  8. min() - Returns the lowest value in an array or a list of numbers.

    • Example: min(2, 3, 9, 4) returns 2.
  9. rand() - Generates a random integer.

    • Example: rand(1, 100) returns a random integer between 1 and 100.
  10. mt_rand() - Generates a better random number (Mersenne Twister algorithm).

    • Example: mt_rand(1, 100).
  11. number_format() - Formats a number with grouped thousands.

    • Example: number_format(1234567.89, 2) returns 1,234,567.89.
  12. is_nan() - Checks whether a value is not a number (NaN).

    • Example: is_nan(NAN) returns true.
  13. fmod() - Returns the floating-point remainder of division.

    • Example: fmod(5.7, 1.3) returns 0.5.
  14. intdiv() - Performs integer division.

    • Example: intdiv(10, 3) returns 3.
  15. log() - Returns the natural logarithm of a number (or logarithm to a specified base).

    • Example: log(8, 2) returns 3.
  16. exp() - Returns e raised to the power of a number (exponential function).

    • Example: exp(1) returns approximately 2.71828.
  17. decbin() - Converts a decimal number to binary.

    • Example: decbin(10) returns 1010.
  18. dechex() - Converts a decimal number to hexadecimal.

    • Example: dechex(255) returns ff.
  19. decoct() - Converts a decimal number to octal.

    • Example: decoct(10) returns 12.
  20. bindec() - Converts a binary number to decimal.

    • Example: bindec('1010') returns 10.
  21. hexdec() - Converts a hexadecimal number to decimal.

    • Example: hexdec('ff') returns 255.
  22. octdec() - Converts an octal number to decimal.

    • Example: octdec('12') returns 10.
  23. pi() - Returns the value of π (pi).

    • Example: pi() returns 3.1415926535898.
  24. is_finite() - Checks if a value is a finite number.

    • Example: is_finite(3.4) returns true.
  25. is_infinite() - Checks if a value is an infinite number.

    • Example: is_infinite(INF) returns true.
  26. is_numeric() - Checks whether a variable is a number or a numeric string.

    • Example: is_numeric("123") returns true.
  27. hypot() - Returns the length of the hypotenuse of a right-angle triangle.

    • Example: hypot(3, 4) returns 5.
  28. deg2rad() - Converts a number from degrees to radians.

    • Example: deg2rad(180) returns 3.14159.
  29. rad2deg() - Converts a number from radians to degrees.

    • Example: rad2deg(pi()) returns 180.
  30. sin() - Returns the sine of a number (angle in radians).

    • Example: sin(pi()/2) returns 1.
  31. cos() - Returns the cosine of a number (angle in radians).

    • Example: cos(pi()) returns -1.
  32. tan() - Returns the tangent of a number (angle in radians).

    • Example: tan(pi()/4) returns 1.
  33. asin() - Returns the arcsine of a number (result in radians).

    • Example: asin(1) returns pi()/2.
  34. acos() - Returns the arccosine of a number (result in radians).

    • Example: acos(1) returns 0.
  35. atan() - Returns the arctangent of a number (result in radians).

    • Example: atan(1) returns pi()/4.
  36. atan2() - Returns the angle (in radians) from the X-axis to a point (Y, X).

    • Example: atan2(1, 1) returns pi()/4.
  37. base_convert() - Converts a number between arbitrary bases.

    • Example: base_convert('A37334', 16, 2) converts from hexadecimal to binary.

These functions allow PHP to handle various mathematical operations, number formatting, and number conversions, making it a powerful tool for building applications that require numeric computations.