0% complete

Operators · Lesson 1 of 6

Arithmetic Operators

Calculate with addition, subtraction, multiplication, division, and remainder.

Lesson

PHP uses +, -, *, /, and % for common arithmetic. Division by zero is invalid, so guard a divisor when it can come from user input. Parentheses make the intended order clear, especially when a calculation mixes multiplication and addition. The remainder operator is useful for recurring patterns such as checking whether a number is even. Further guidance Evaluate an expression from its grouped parts and add parentheses whenever precedence is not obvious. Check divisors before division, be cautious with floating-point equality, and avoid changing state inside a complicated expression. Small, named intermediate values make calculations easier to verify. Practice task: Create a fresh example for Arithmetic Operators. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

Arithmetic Operators
PHP
$price = 12;
$quantity = 3;
$total = $price * $quantity;
echo $total, ' ', ($quantity % 2 === 1 ? 'odd count' : 'even count');
Expected output
36 odd count

Key points

  • Calculate with addition, subtraction, multiplication, division, and remainder.
  • Make calculation order explicit with parentheses and named intermediate values.
  • Check edge cases such as zero divisors and floating-point precision.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.