PHP Arithmetic Operators: A Beginner's Guide

Hello, future PHP maestros! Today, we're going to embark on an exciting journey into the world of PHP arithmetic operators. Don't worry if you've never written a line of code before – I'll be your friendly guide through this numerical adventure!

PHP - Arithmatic Operators

What Are Arithmetic Operators?

Before we dive in, let's understand what arithmetic operators are. Remember those math symbols you learned in school? Addition (+), subtraction (-), multiplication (*), and division (/)? Well, in PHP, we use these same symbols to perform calculations. Cool, right? It's like your computer is a giant calculator!

Basic Arithmetic Operators

Let's start with the basics. Here's a table of the most common arithmetic operators in PHP:

Operator Name Example
+ Addition $a + $b
- Subtraction $a - $b
* Multiplication $a * $b
/ Division $a / $b
% Modulus $a % $b

Now, let's see these operators in action!

Addition (+)

<?php
$a = 5;
$b = 3;
$result = $a + $b;
echo "5 + 3 = " . $result;
?>

When you run this code, it will output: 5 + 3 = 8

What's happening here? We're declaring two variables, $a and $b, and assigning them values. Then, we're using the addition operator (+) to add these values together and storing the result in a new variable called $result. Finally, we're printing out the result using echo.

Subtraction (-)

<?php
$a = 10;
$b = 4;
$result = $a - $b;
echo "10 - 4 = " . $result;
?>

Output: 10 - 4 = 6

Similar to addition, but this time we're subtracting $b from $a.

Multiplication (*)

<?php
$a = 6;
$b = 7;
$result = $a * $b;
echo "6 * 7 = " . $result;
?>

Output: 6 * 7 = 42

Here, we're multiplying $a and $b. Remember, in PHP (and most programming languages), we use * for multiplication, not x.

Division (/)

<?php
$a = 20;
$b = 4;
$result = $a / $b;
echo "20 / 4 = " . $result;
?>

Output: 20 / 4 = 5

Division works just like you'd expect. But be careful! If you try to divide by zero, PHP will throw an error. Always check your divisor!

Modulus (%)

<?php
$a = 17;
$b = 5;
$result = $a % $b;
echo "17 % 5 = " . $result;
?>

Output: 17 % 5 = 2

The modulus operator gives you the remainder after division. In this case, 17 divided by 5 is 3 with a remainder of 2. That's why our result is 2.

Combining Operators

Now that we've mastered the basics, let's mix things up a bit!

<?php
$a = 10;
$b = 5;
$c = 2;
$result = $a + $b * $c;
echo "10 + 5 * 2 = " . $result;
?>

Output: 10 + 5 * 2 = 20

Wait a minute! Shouldn't that be 30? Well, just like in math class, PHP follows the order of operations. Multiplication happens before addition, so it's actually calculating 10 + (5 * 2).

If we want to change the order, we can use parentheses:

<?php
$a = 10;
$b = 5;
$c = 2;
$result = ($a + $b) * $c;
echo "(10 + 5) * 2 = " . $result;
?>

Output: (10 + 5) * 2 = 30

Now that's more like it!

Real-World Example: Calculating a Discount

Let's put our new skills to use with a real-world example. Imagine you're building an e-commerce website, and you want to calculate the final price of an item after applying a discount.

<?php
$originalPrice = 100;  // The original price of the item
$discountPercentage = 20;  // The discount percentage

// Calculate the discount amount
$discountAmount = $originalPrice * ($discountPercentage / 100);

// Calculate the final price
$finalPrice = $originalPrice - $discountAmount;

echo "Original Price: $" . $originalPrice . "\n";
echo "Discount Percentage: " . $discountPercentage . "%\n";
echo "You Save: $" . $discountAmount . "\n";
echo "Final Price: $" . $finalPrice;
?>

Output:

Original Price: $100
Discount Percentage: 20%
You Save: $20
Final Price: $80

In this example, we're using multiple arithmetic operators to calculate a discount. We first calculate the discount amount by multiplying the original price by the discount percentage (converted to a decimal). Then, we subtract this amount from the original price to get the final price.

Conclusion

Congratulations! You've just taken your first steps into the world of PHP arithmetic operators. Remember, practice makes perfect, so don't be afraid to experiment with these operators. Try creating your own calculations, maybe a tip calculator or a body mass index (BMI) calculator.

PHP arithmetic operators are the building blocks for more complex calculations in your programs. As you continue your PHP journey, you'll find yourself using these operators frequently. So, keep calculating, keep coding, and most importantly, keep having fun!

Credits: Image by storyset