PHP Tutorial



PHP MATH


🔢 PHP Math

PHP provides powerful built-in mathematical functions that allow you to perform basic to advanced math operations easily.

💡 Tip: PHP automatically treats numbers as integers or floats depending on the value.

📘 Basic Arithmetic Operators

PHP supports all standard arithmetic operations:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (Remainder)
<?php
  $a = 15;
  $b = 4;

  echo $a + $b; // 19
  echo $a - $b; // 11
  echo $a * $b; // 60
  echo $a / $b; // 3.75
  echo $a % $b; // 3
?>
  

📚 Common Math Functions in PHP

  • abs(x) – Returns the absolute (positive) value
  • round(x) – Rounds a number
  • ceil(x) – Rounds up
  • floor(x) – Rounds down
  • sqrt(x) – Returns the square root
  • pow(x, y) – Raises x to the power y
  • max(x, y...) – Returns the highest value
  • min(x, y...) – Returns the lowest value
  • rand(min, max) – Returns a random number

🔍 Example:

<?php
  echo abs(-20);       // 20
  echo round(3.4);     // 3
  echo ceil(3.4);      // 4
  echo floor(3.9);     // 3
  echo sqrt(16);       // 4
  echo pow(2, 3);      // 8
  echo max(4, 7, 1);   // 7
  echo min(4, 7, 1);   // 1
  echo rand(10, 99);   // Random number between 10–99
?>
  
🎲 Random Tip: You can use rand() to build games or lucky draw apps!

✅ Summary:

  • Basic arithmetic: +, -, *, /, %
  • Useful functions: round(), abs(), sqrt(), rand(), etc.
  • Perfect for calculations, games, and data processing

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review