PHP Math

PHP provides a variety of built-in math functions that allow you to perform mathematical operations and calculations. Here are some commonly used math functions in PHP:

  1. Basic Math Operations:
    • Addition: echo 2 + 3; // Output: 5
    • Subtraction: echo 5 - 2; // Output: 3
    • Multiplication: echo 2 * 3; // Output: 6
    • Division: echo 10 / 2; // Output: 5
    • Modulus (Remainder): echo 10 % 3; // Output: 1
  2. Exponentiation:
    • Power: echo pow(2, 3); // Output: 8
    • Square Root: echo sqrt(16); // Output: 4
    • Cube Root: echo pow(27, 1/3); // Output: 3
  3. Rounding Functions:
    • Round: echo round(3.7); // Output: 4
    • Floor (Round down): echo floor(3.7); // Output: 3
    • Ceiling (Roundup): echo ceil(3.2); // Output: 4
  4. Trigonometric Functions:
    • Sine: echo sin(0.5);
    • Cosine: echo cos(0.5);
    • Tangent: echo tan(0.5);
  5. Logarithmic Functions:
    • Natural Logarithm: echo log(10);
    • Logarithm (base 10): echo log10(100);
  6. Random Number Generation:
    • Random Integer: echo rand(1, 100); // Output: Random number between 1 and 100
    • Random Float: echo rand() / getrandmax(); // Output: Random float between 0 and 1

These are just a few examples of the math functions available in PHP. There are many more mathematical functions provided by PHP, such as trigonometric, exponential, logarithmic, and statistical functions. You can refer to the official PHP documentation for a comprehensive list of math functions and their usage.

Reference document