➕ PHP Operators
Estimated reading: 2 minutes 266 views

PHP Arithmetic Operators Explained – Add, Subtract, Multiply & More


Introduction – Why Use Arithmetic Operators in PHP?

Arithmetic operations are fundamental to any programming language, and PHP provides a simple, intuitive set of operators for mathematical calculations. Whether you’re building a billing system, statistics engine, or a simple calculator, arithmetic operators help you perform operations like addition, subtraction, multiplication, division, and modulus.

In this section, you’ll learn:

  • Types of arithmetic operators in PHP
  • Syntax and usage of each operator
  • Examples and output for clarity

PHP Arithmetic Operators Table

OperatorNameDescriptionExampleResult
+AdditionAdds two numbers$x + $ySum
-SubtractionSubtracts the right operand from the left$x - $yDifference
*MultiplicationMultiplies two numbers$x * $yProduct
/DivisionDivides the left operand by the right$x / $yQuotient
%ModulusReturns the remainder after division$x % $yRemainder

Examples of Arithmetic Operations

<?php
$x = 10;
$y = 4;

echo $x + $y;  //  14
echo $x - $y;  //  6
echo $x * $y;  // ✖️ 40
echo $x / $y;  // 2.5
echo $x % $y;  //  2
?>

Output

14
6
40
2.5
2

Key Notes

  • Division by zero will throw a warning in PHP.
  • The modulus operator % is often used in loops and conditions.
  • Use round() if you want to round off decimal results from division.

Summary – Recap & Next Steps

Key Takeaways:

  • PHP supports five core arithmetic operators.
  • They work with integers and floating-point numbers.
  • Ideal for any mathematical or statistical calculations in your application.

Real-World Use Cases:

  • E-commerce totals and discounts
  • Formulas in dashboards and analytics
  • Game scores and level calculations

FAQ – PHP Arithmetic Operators

Can I use arithmetic operators with strings in PHP?
PHP tries to convert strings to numbers when possible. However, it’s best to cast explicitly using (int) or (float).

What happens if I divide by zero in PHP?
PHP issues a warning and returns INF (infinity) or false depending on version and context.

Can I chain arithmetic operations like $x + $y * $z?
Yes. PHP respects standard operator precedence (multiplication and division first, then addition and subtraction).

Are these operators only for numbers?
Primarily yes, though PHP will attempt type juggling if mixed types are involved.


Share Now :
Share

➕ PHP — Arithmetic Operators

Or Copy Link

CONTENTS
Scroll to Top