Lesson 4

Welcome back! In our previous lesson, you learned about variables and data types in PHP. Now, it's time to take a significant step forward by exploring **basic mathematical operations**. These operations are foundational in programming and are used in a wide range of applications, from handling simple arithmetic to performing complex calculations.

In this lesson, you will learn how to perform **basic mathematical operations** in PHP, such as `addition`

, `subtraction`

, `multiplication`

, and `division`

. These operations are essential for manipulating numerical data and are frequently used in various programming scenarios. Here's a glimpse of what you'll encounter:

php`1<?php 2$fuelInitial = 1000; // in liters 3$fuelAdded = 500; // in liters 4$fuelUsed = 300; // in liters 5 6$totalFuel = $fuelInitial + $fuelAdded; // Addition 7echo "Total fuel after adding: " . $totalFuel . " liters\n"; 8 9$remainingFuel = $totalFuel - $fuelUsed; // Subtraction 10echo "Remaining fuel after usage: " . $remainingFuel . " liters\n"; 11 12$fuelDouble = $fuelInitial * 2; // Multiplication 13echo "Double the initial fuel: " . $fuelDouble . " liters\n"; 14 15$fuelPerAstronaut = $totalFuel / 5; // Division (for 5 astronauts) 16echo "Fuel per astronaut: " . $fuelPerAstronaut . " liters\n"; 17?>`

By the end of this lesson, you will be able to perform these basic operations with confidence and see how they can be applied to real-world problems.

Understanding basic mathematical operators is crucial for any programmer. These operations allow you to manage numbers and perform calculations, which are fundamental tasks in most coding projects. Whether you're calculating totals, balancing budgets, or managing resources in a game, these operators are indispensable tools in your programming toolkit.

Mastering these operations will also help you understand more complex concepts in the future, such as algorithms, data analysis, and computer graphics. They serve as building blocks for almost every programming task you will encounter, making them essential to your skill set.

Are you excited to see how math can power your PHP programs? Let's dive into the practice section and start applying these operators in meaningful ways.