Example 3: Basic Calculator
Math operators and calculations in PHP
Download file calculadora🎯 Live Demo
Operation results:
Change the numbers!
Quick numbers:
📖 Code Explanation
Complete PHP Code
<?php
$numero1 = 15;
$numero2 = 5;
$suma = $numero1 + $numero2;
$resta = $numero1 - $numero2;
$multiplicacion = $numero1 * $numero2;
$division = $numero1 / $numero2;
echo "La suma es: $suma";
echo "La resta es: $resta";
echo "La multiplicación es: $multiplicacion";
echo "La división es: $division";
?>
📝 Lines 1-2: Numeric variable declaration
$numero1 = 15;$numero2 = 5;
What does it do? Creates two numeric variables that will serve as operands for the operations.
Why is it done this way? In PHP, numbers are assigned directly without quotes. PHP automatically detects that they are numbers.
💡 Concept: Numeric types
PHP has two main types of numbers:
Integer: Whole numbers (15, -3, 0)
Float: Decimal numbers (3.14, -2.5, 0.0)
📝 Lines 4-7: Math operations
$suma = $numero1 + $numero2;$resta = $numero1 - $numero2;$multiplicacion = $numero1 * $numero2;$division = $numero1 / $numero2;
What does it do? Performs the four basic math operations and stores the results in variables.
Why is it done this way? Each operation uses a specific math operator and stores the result for later use.
💡 Concept: Arithmetic operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo (division remainder)
📝 Lines 9-12: Display results
echo "La suma es: $suma";echo "La resta es: $resta";echo "La multiplicación es: $multiplicacion";echo "La división es: $division";
What does it do? Displays each result with a descriptive message.
Why is it done this way? Uses variable interpolation to create clear messages that include both the text and the calculated results.
💡 Concept: Operator precedence
PHP follows standard math rules:
1. Parentheses ()
2. Multiplication and division (* /)
3. Addition and subtraction (+ -)
🔢 Math Operators in PHP
Addition
Adds two values
$resultado = 5 + 3; // 8
Subtraction
Subtracts the second value from the first
$resultado = 5 - 3; // 2
Multiplication
Multiplies two values
$resultado = 5 * 3; // 15
Division
Divides the first value by the second
$resultado = 6 / 3; // 2
Modulo
Returns the remainder of a division
$resultado = 5 % 3; // 2
Exponentiation
Raises a number to a power
$resultado = 2 ** 3; // 8
🎯 Key Concepts Learned
Numeric Variables
- Assigned without quotes
- PHP automatically detects the type
- Can be integers or floats
- Operations can be performed directly
Arithmetic Operators
- Special symbols for operations
- Follow math precedence
- Can use variables and numbers
- Return a result
Error Handling
- Division by zero causes an error
- Use conditionals to prevent it
- Validate data before operating
- Show clear error messages
💪 Practical Exercise
Extend the calculator
Practice these changes to improve your skills:
- Add the modulo operation (%) to the calculator
- Implement exponentiation (**) to calculate powers
- Create an operation that calculates the average of 3 numbers
- Add validation to prevent division by zero
- Calculate the area of a circle (π × radio²)
Solution 1: Modulo
<?php
$modulo = $numero1 % $numero2;
echo "El módulo es: $modulo";
?>
Solution 2: Exponentiation
<?php
$potencia = $numero1 ** $numero2;
echo "La potencia es: $potencia";
?>
Solution 3: Average
<?php
$numero3 = 10;
$promedio = ($numero1 + $numero2 + $numero3) / 3;
echo "El promedio es: $promedio";
?>
Solution 4: Division validation
<?php
if ($numero2 != 0) {
$division = $numero1 / $numero2;
echo "La división es: $division";
} else {
echo "Error: No se puede dividir por cero";
}
?>
Solution 5: Area of the circle
<?php
$radio = 5;
$area = pi() * ($radio ** 2);
echo "El área del círculo es: " . round($area, 2);
?>