Skip to main content

Course Progress

Loading...

Module 2: PHP Arithmetic Operators

Duration: 30 minutes
Module 2: PHP Operators

Learning Objectives

  • Master PHP operators
  • Understand operator precedence
  • Apply operators in practical scenarios
  • Write efficient expressions

Introduction to PHP Arithmetic Operators

Welcome to our session on PHP Arithmetic Operators! These operators are the foundation of mathematical operations in PHP and form an essential part of your programming toolkit. Whether you're calculating product prices, processing form data, or developing complex algorithms, understanding arithmetic operators is crucial.

Think of arithmetic operators as the basic tools in a calculator. Just like a calculator helps you perform mathematical operations, PHP arithmetic operators allow your programs to perform calculations on numeric values. Today, we'll explore these operators, understand how they work, and see them in action through practical examples.

Basic Arithmetic Operators

PHP provides several arithmetic operators that perform mathematical operations similar to what you'd do with a basic calculator:

+ Addition - Subtraction * Multiplication / Division % Modulus ** Exponentiation intdiv() Integer Division <?php ?> PHP Tags
Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y power

Let's look at each operator in more detail.

Addition Operator (+)

The addition operator (+) adds two values together.

Basic Addition Example

<?php
// Simple addition
$a = 10;
$b = 20;
$sum = $a + $b;
echo "Sum: " . $sum; // Outputs: Sum: 30

// Adding decimals
$price = 19.99;
$tax = 1.60;
$total = $price + $tax;
echo "Total price: $" . $total; // Outputs: Total price: $21.59
?>

Real-World Application: Shopping Cart

In an e-commerce website, the addition operator is essential for calculating the total cost of items in a shopping cart:

<?php
// Shopping cart calculation
$item1_price = 29.99;
$item2_price = 49.99;
$item3_price = 15.50;

// Calculate subtotal
$subtotal = $item1_price + $item2_price + $item3_price;

// Calculate tax (8%)
$tax_rate = 0.08;
$tax_amount = $subtotal * $tax_rate;

// Calculate total
$total = $subtotal + $tax_amount;

echo "Subtotal: $" . number_format($subtotal, 2);
echo "<br>";
echo "Tax: $" . number_format($tax_amount, 2);
echo "<br>";
echo "Total: $" . number_format($total, 2);
?>

Analogy: Think of the addition operator as stacking blocks. If you have 5 blocks and add 3 more, you now have 8 blocks in total.

Note: PHP's addition operator can also be used with strings containing numeric values. PHP will automatically convert the string to a number before performing the operation.

<?php
$num_string = "42";
$num = 8;
$result = $num_string + $num;
echo $result; // Outputs: 50
?>

Subtraction Operator (-)

The subtraction operator (-) subtracts one value from another.

Basic Subtraction Example

<?php
// Simple subtraction
$a = 50;
$b = 30;
$difference = $a - $b;
echo "Difference: " . $difference; // Outputs: Difference: 20

// Negative results
$c = 10;
$d = 25;
$result = $c - $d;
echo "Result: " . $result; // Outputs: Result: -15
?>

Real-World Application: Inventory Management

The subtraction operator is useful for tracking inventory after sales:

<?php
// Inventory management
$initial_stock = 100;
$items_sold = 37;

// Calculate remaining inventory
$current_stock = $initial_stock - $items_sold;

echo "Initial stock: " . $initial_stock;
echo "<br>";
echo "Items sold: " . $items_sold;
echo "<br>";
echo "Current stock: " . $current_stock;

// Check if reorder is needed
$reorder_threshold = 25;
if ($current_stock <= $reorder_threshold) {
    echo "<br>";
    echo "Stock low! Time to reorder.";
}
?>

Analogy: Think of subtraction as removing apples from a basket. If you have 10 apples and remove 4, you're left with 6 apples.

Multiplication Operator (*)

The multiplication operator (*) multiplies two values together.

Basic Multiplication Example

<?php
// Simple multiplication
$a = 6;
$b = 7;
$product = $a * $b;
echo "Product: " . $product; // Outputs: Product: 42

// Multiplying decimals
$price = 24.99;
$quantity = 3;
$total = $price * $quantity;
echo "Total cost: $" . $total; // Outputs: Total cost: $74.97
?>

Real-World Application: Discount Calculator

The multiplication operator is perfect for calculating discounts on products:

<?php
// Discount calculator
$original_price = 79.99;
$discount_percentage = 0.25; // 25% discount

// Calculate discount amount
$discount_amount = $original_price * $discount_percentage;

// Calculate final price
$final_price = $original_price - $discount_amount;

echo "Original price: $" . number_format($original_price, 2);
echo "<br>";
echo "Discount percentage: " . ($discount_percentage * 100) . "%";
echo "<br>";
echo "Discount amount: $" . number_format($discount_amount, 2);
echo "<br>";
echo "Final price: $" . number_format($final_price, 2);
?>

Analogy: Multiplication is like repeated addition. If you need 5 packages of cookies and each package has 8 cookies, you'll have 5 × 8 = 40 cookies in total.

Optimization tip: When multiplying by powers of 2, you can alternatively use bitwise left shift operators for better performance in certain situations:

<?php
$num = 5;
$result1 = $num * 2;  // Regular multiplication
$result2 = $num << 1; // Bitwise left shift (same as multiplying by 2)

echo "5 * 2 = " . $result1 . "<br>"; // Outputs: 5 * 2 = 10
echo "5 << 1 = " . $result2;              // Outputs: 5 << 1 = 10

// Multiplying by 4 (2^2)
$result3 = $num * 4;  // Regular multiplication
$result4 = $num << 2; // Bitwise left shift (same as multiplying by 4)

echo "<br>5 * 4 = " . $result3;      // Outputs: 5 * 4 = 20
echo "<br>5 << 2 = " . $result4;     // Outputs: 5 << 2 = 20
?>

Division Operator (/)

The division operator (/) divides one value by another.

Basic Division Example

<?php
// Simple division
$a = 100;
$b = 5;
$quotient = $a / $b;
echo "Quotient: " . $quotient; // Outputs: Quotient: 20

// Division with decimals
$c = 10;
$d = 3;
$result = $c / $d;
echo "<br>";
echo "Result: " . $result; // Outputs: Result: 3.3333333333333
echo "<br>";
echo "Formatted result: " . number_format($result, 2); // Outputs: Formatted result: 3.33
?>

Real-World Application: Average Calculation

The division operator is commonly used to calculate averages, such as in grade calculations:

<?php
// Calculate average test score
$test1 = 85;
$test2 = 92;
$test3 = 78;
$test4 = 88;

// Find the average
$total_tests = 4;
$total_score = $test1 + $test2 + $test3 + $test4;
$average = $total_score / $total_tests;

echo "Test 1: " . $test1;
echo "<br>";
echo "Test 2: " . $test2;
echo "<br>";
echo "Test 3: " . $test3;
echo "<br>";
echo "Test 4: " . $test4;
echo "<br>";
echo "Average: " . number_format($average, 1); // Outputs: Average: 85.8

// Determine letter grade
$letter_grade = "";
if ($average >= 90) {
    $letter_grade = "A";
} elseif ($average >= 80) {
    $letter_grade = "B";
} elseif ($average >= 70) {
    $letter_grade = "C";
} elseif ($average >= 60) {
    $letter_grade = "D";
} else {
    $letter_grade = "F";
}

echo "<br>";
echo "Letter Grade: " . $letter_grade; // Outputs: Letter Grade: B
?>

Analogy: Division is like distributing items equally among groups. If you have 12 candies and want to share them equally among 3 children, each child gets 12 ÷ 3 = 4 candies.

Warning: Division by Zero

In PHP, division by zero will result in a warning. Always check if the divisor is zero before performing division:

<?php
// Unsafe division
$numerator = 10;
$denominator = 0;

// This will generate a warning
// $result = $numerator / $denominator;

// Safe division with error handling
if ($denominator != 0) {
    $result = $numerator / $denominator;
    echo "Result: " . $result;
} else {
    echo "Error: Cannot divide by zero";
}
?>

Note: In PHP 7 and later, division by zero results in a DivisionByZeroError exception that can be caught with try/catch:

<?php
$numerator = 10;
$denominator = 0;

try {
    $result = $numerator / $denominator;
    echo "Result: " . $result;
} catch (DivisionByZeroError $e) {
    echo "Error: " . $e->getMessage();
}
?>

Modulus Operator (%)

The modulus operator (%) returns the remainder after division of one number by another.

Basic Modulus Example

<?php
// Simple modulus
$a = 17;
$b = 5;
$remainder = $a % $b;
echo "Remainder of $a ÷ $b: " . $remainder; // Outputs: Remainder of 17 ÷ 5: 2

// Even/odd check
$number = 42;
$is_even = ($number % 2 == 0);
echo "<br>";
echo "Is $number even? " . ($is_even ? "Yes" : "No"); // Outputs: Is 42
 even? Yes
?>
Diagram
> C[Quotient: 3] B 17 Divide by 5 Quotient: 3 Remainder: 2 Modulus result: 2

Real-World Applications of Modulus

Application 1: Alternating Row Colors

The modulus operator is perfect for creating alternating row colors in tables:

<?php
echo "<table border='1'>";
for ($i = 1; $i <= 10; $i++) {
    // Use modulus to alternate row colors
    $row_class = ($i % 2 == 0) ? "even-row" : "odd-row";
    $bgcolor = ($i % 2 == 0) ? "#f2f2f2" : "#ffffff";
    
    echo "<tr style='background-color: $bgcolor;'>";
    echo "<td>Row $i</td>";
    echo "<td>This is an " . (($i % 2 == 0) ? "even" : "odd") . " row</td>";
    echo "</tr>";
}
echo "</table>";
?>

Application 2: Circular Patterns

Modulus helps create circular or repeating patterns:

<?php
// Display hours on a 12-hour clock
for ($hour = 0; $hour < 24; $hour++) {
    $display_hour = $hour % 12;
    if ($display_hour == 0) {
        $display_hour = 12;
    }
    
    $period = ($hour < 12) ? "AM" : "PM";
    echo "$hour:00 in 24-hour format is $display_hour:00 $period<br>";
}
?>

Application 3: Pagination

Modulus is useful for calculating pagination:

<?php
$total_items = 257;
$items_per_page = 10;

// Calculate total pages
$total_pages = ceil($total_items / $items_per_page);

// Display pagination info
echo "Total items: $total_items<br>";
echo "Items per page: $items_per_page<br>";
echo "Total pages needed: $total_pages<br><br>";

// Display page details
for ($page = 1; $page <= $total_pages; $page++) {
    $start_item = (($page - 1) * $items_per_page) + 1;
    $end_item = min($page * $items_per_page, $total_items);
    
    echo "Page $page shows items $start_item to $end_item<br>";
}
?>

Analogy: Think of modulus as measuring what's left over after you've packed items into equal-sized containers. If you have 23 apples and pack them into boxes that hold 5 apples each, you'll fill 4 boxes completely (20 apples) and have 3 apples left over. The modulus (23 % 5) gives you that remainder: 3.

Exponentiation Operator (**)

The exponentiation operator (**) raises a number to the power of another number. This operator was introduced in PHP 5.6.

Basic Exponentiation Example

<?php
// Simple exponentiation
$base = 2;
$exponent = 3;
$result = $base ** $exponent;
echo "$base raised to the power of $exponent: " . $result; // Outputs: 2 raised to the power of 3: 8

// Alternative using pow() function
$result2 = pow($base, $exponent);
echo "<br>";
echo "Using pow() function: " . $result2; // Outputs: Using pow() function: 8

// Negative exponents
$negative_exp = $base ** (-1);
echo "<br>";
echo "$base raised to the power of -1: " . $negative_exp; // Outputs: 2 raised to the power of -1: 0.5
?>

Real-World Application: Compound Interest

Exponentiation is essential for financial calculations like compound interest:

<?php
// Compound interest calculator
$principal = 1000;  // Initial investment
$rate = 0.05;       // 5% annual interest rate
$years = 10;        // Time period in years
$times_per_year = 1; // Compounding frequency (1 = annual)

// Calculate final amount using compound interest formula: A = P(1 + r/n)^(nt)
$final_amount = $principal * (1 + $rate/$times_per_year) ** ($times_per_year * $years);

echo "Initial investment: $" . number_format($principal, 2);
echo "<br>";
echo "Annual interest rate: " . ($rate * 100) . "%";
echo "<br>";
echo "Time period: $years years";
echo "<br>";
echo "Final amount: $" . number_format($final_amount, 2); // Outputs: Final amount: $1,628.89

// Compare with different compounding frequencies
$compounding_options = [
    1 => "annually",
    2 => "semi-annually",
    4 => "quarterly",
    12 => "monthly",
    365 => "daily"
];

echo "<br><br>";
echo "Effect of different compounding frequencies:<br>";

foreach ($compounding_options as $frequency => $label) {
    $amount = $principal * (1 + $rate/$frequency) ** ($frequency * $years);
    echo "Compounding $label: $" . number_format($amount, 2) . "<br>";
}
?>

Analogy: Exponentiation is like multiplication repeated multiple times. If 23 means 2 × 2 × 2, then it's like multiplying 2 by itself 3 times, resulting in 8.

Integer Division (intdiv())

While not an operator but a function, intdiv() performs integer division, returning the integer quotient of the dividend divided by the divisor (introduced in PHP 7).

Integer Division Example

<?php
// Integer division
$a = 17;
$b = 5;

// Regular division
$regular_result = $a / $b;
echo "Regular division: $a / $b = " . $regular_result; // Outputs: Regular division: 17 / 5 = 3.4

// Integer division
$int_result = intdiv($a, $b);
echo "<br>";
echo "Integer division: intdiv($a, $b) = " . $int_result; // Outputs: Integer division: intdiv(17, 5) = 3

// Another example
$c = -10;
$d = 3;
$int_result2 = intdiv($c, $d);
echo "<br>";
echo "Integer division: intdiv($c, $d) = " . $int_result2; // Outputs: Integer division: intdiv(-10, 3) = -3
?>

Real-World Application: Time Conversion

Integer division is useful for converting between time units:

<?php
// Convert seconds to hours, minutes, and seconds
$total_seconds = 7384; // 2 hours, 3 minutes, 4 seconds

// Calculate hours
$hours = intdiv($total_seconds, 3600);
$remainder = $total_seconds % 3600;

// Calculate minutes
$minutes = intdiv($remainder, 60);
$seconds = $remainder % 60;

echo "$total_seconds seconds = $hours hours, $minutes minutes, $seconds seconds";
// Outputs: 7384 seconds = 2 hours, 3 minutes, 4 seconds

// Another example: Calculate full weeks and days
$total_days = 23;

$weeks = intdiv($total_days, 7);
$days = $total_days % 7;

echo "<br>";
echo "$total_days days = $weeks weeks and $days days";
// Outputs: 23 days = 3 weeks and 2 days
?>

Analogy: Integer division is like counting how many complete containers you can fill. If you're putting 17 apples into bags that hold 5 apples each, you can fill 3 complete bags (the integer division result), with 2 apples left over (the remainder).

Operator Precedence

When using multiple operators in a single expression, PHP follows rules for the order in which operations are performed, known as operator precedence.

Arithmetic Operator Precedence (Highest to Lowest)

Diagram
> C[3. * / % Multiplication, Division, Modulus] C 1. () Parentheses 2. ** Exponentiation 3. * / % Multiplication, Division, Modulus 4. + - Addition, Subtraction

Operator Precedence Examples

<?php
// Example 1: Order of operations
$result1 = 5 + 3 * 2;
echo "5 + 3 * 2 = " . $result1; // Outputs: 5 + 3 * 2 = 11 (multiplication before addition)

// Example 2: Using parentheses to change precedence
$result2 = (5 + 3) * 2;
echo "<br>";
echo "(5 + 3) * 2 = " . $result2; // Outputs: (5 + 3) * 2 = 16 (parentheses first)

// Example 3: Complex expression
$result3 = 10 + 20 / 5 ** 2 - 3;
echo "<br>";
echo "10 + 20 / 5 ** 2 - 3 = " . $result3; // Outputs: 10 + 20 / 5 ** 2 - 3 = 7.8
// Calculation: 10 + 20 / 25 - 3 = 10 + 0.8 - 3 = 7.8
?>

Best Practice: Use parentheses to make your code more readable and to ensure the operations are performed in the order you intend, even when default precedence would give the same result.

<?php
// Using parentheses for clarity
$price = 100;
$tax_rate = 0.08;
$discount = 20;

// Without parentheses (works correctly, but harder to read)
$total = $price + $price * $tax_rate - $discount;

// With parentheses (clearer intent)
$total_clear = $price + ($price * $tax_rate) - $discount;

echo "Total: $" . $total; // Outputs: Total: $88
echo "<br>";
echo "Total (with clearer parentheses): $" . $total_clear; // Same result, more readable code
?>

Combined Arithmetic Operations

Let's look at some examples that combine multiple arithmetic operators to solve real-world problems.

Example 1: Recipe Scaling

<?php
// Original recipe for 4 people
$flour_cups = 2.5;
$sugar_cups = 1;
$eggs = 2;
$milk_cups = 0.75;

// Scale the recipe for different numbers of people
$servings = [2, 6, 10, 15];

echo "<h4>Recipe Scaling Calculator</h4>";
echo "Original recipe (serves 4):<br>";
echo "- Flour: " . $flour_cups . " cups<br>";
echo "- Sugar: " . $sugar_cups . " cups<br>";
echo "- Eggs: " . $eggs . "<br>";
echo "- Milk: " . $milk_cups . " cups<br><br>";

foreach ($servings as $people) {
    $scale_factor = $people / 4;
    
    echo "<strong>Recipe for $people people:</strong><br>";
    echo "- Flour: " . number_format($flour_cups * $scale_factor, 2) . " cups<br>";
    echo "- Sugar: " . number_format($sugar_cups * $scale_factor, 2) . " cups<br>";
    
    // For eggs, we need to round to the nearest whole number
    $scaled_eggs = round($eggs * $scale_factor);
    echo "- Eggs: " . $scaled_eggs . "<br>";
    
    echo "- Milk: " . number_format($milk_cups * $scale_factor, 2) . " cups<br><br>";
}
?>

Example 2: Monthly Payment Calculator

<?php
// Loan calculator
$loan_amount = 250000; // $250,000 loan
$annual_interest_rate = 0.045; // 4.5%
$loan_term_years = 30;

// Convert annual interest rate to monthly
$monthly_interest_rate = $annual_interest_rate / 12;

// Convert loan term to months
$loan_term_months = $loan_term_years * 12;

// Calculate monthly payment using the loan formula
// M = P * (r * (1 + r)^n) / ((1 + r)^n - 1)
$numerator = $monthly_interest_rate * ((1 + $monthly_interest_rate) ** $loan_term_months);
$denominator = ((1 + $monthly_interest_rate) ** $loan_term_months) - 1;
$monthly_payment = $loan_amount * ($numerator / $denominator);

echo "<h4>Mortgage Calculator</h4>";
echo "Loan amount: $" . number_format($loan_amount, 2) . "<br>";
echo "Annual interest rate: " . ($annual_interest_rate * 100) . "%<br>";
echo "Loan term: " . $loan_term_years . " years<br>";
echo "Monthly payment: $" . number_format($monthly_payment, 2) . "<br>";

// Calculate total payment and interest paid
$total_paid = $monthly_payment * $loan_term_months;
$total_interest = $total_paid - $loan_amount;

echo "Total amount paid: $" . number_format($total_paid, 2) . "<br>";
echo "Total interest paid: $" . number_format($total_interest, 2) . "<br>";

// Calculate amortization schedule for the first year
echo "<br><h4>First Year Amortization Schedule</h4>";
echo "<table border='1'>";
echo "<tr><th>Month</th><th>Payment</th><th>Principal</th><th>Interest</th><th>Remaining Balance</th></tr>";

$balance = $loan_amount;

for ($month = 1; $month <= 12; $month++) {
    // Calculate interest for this month
    $interest_payment = $balance * $monthly_interest_rate;
    
    // Calculate principal for this month
    $principal_payment = $monthly_payment - $interest_payment;
    
    // Update balance
    $balance -= $principal_payment;
    
    // Output row
    echo "<tr>";
    echo "<td>$month</td>";
    echo "<td>$" . number_format($monthly_payment, 2) . "</td>";
    echo "<td>$" . number_format($principal_payment, 2) . "</td>";
    echo "<td>$" . number_format($interest_payment, 2) . "</td>";
    echo "<td>$" . number_format($balance, 2) . "</td>";
    echo "</tr>";
}

echo "</table>";
?>

Common Pitfalls and Best Practices

Floating Point Precision

PHP, like most programming languages, can have precision issues with floating-point calculations:

<?php
// Precision issues example
$a = 0.1;
$b = 0.2;
$sum = $a + $b;

echo "0.1 + 0.2 = " . $sum; // Might output: 0.1 + 0.2 = 0.30000000000000004

// Better approach for money calculations
$price1 = 0.1 * 100; // 10 cents
$price2 = 0.2 * 100; // 20 cents
$total_cents = $price1 + $price2; // 30 cents
$total_dollars = $total_cents / 100; // $0.30

echo "<br>";
echo "Using cents for calculation: $" . $total_dollars;
?>

Division by Zero

Always check for division by zero to avoid errors or warnings:

<?php
function safe_divide($numerator, $denominator) {
    // Check for division by zero
    if ($denominator == 0) {
        return "Error: Cannot divide by zero";
    }
    
    return $numerator / $denominator;
}

echo safe_divide(10, 2); // Outputs: 5
echo "<br>";
echo safe_divide(10, 0); // Outputs: Error: Cannot divide by zero
?>

Type Juggling

PHP will automatically convert between types when performing arithmetic operations:

<?php
$number = "42"; // String
$result = $number + 8; // PHP converts string to integer
echo "\"42\" + 8 = " . $result; // Outputs: "42" + 8 = 50

// This can sometimes lead to unexpected results
$value = "42hello";
$sum = $value + 10;
echo "<br>";
echo "\"42hello\" + 10 = " . $sum; // Outputs: "42hello" + 10 = 52 (PHP uses only the numeric part)

// Best practice: Explicitly convert strings to numbers
$string_number = "42";
$proper_number = (int)$string_number; // Explicitly cast to integer
$proper_result = $proper_number + 8;
echo "<br>";
echo "(int)\"42\" + 8 = " . $proper_result; // Outputs: (int)"42" + 8 = 50
?>

Best Practices for Arithmetic Operations

  • Use Parentheses: Make your code readable and ensure operations occur in the intended order.
  • Format Decimal Output: Use number_format() for displaying decimal values with proper precision.
  • Be Careful with Floating Point: For financial calculations, consider working with cents as integers.
  • Validate Input: Always validate and sanitize user input before using it in calculations.
  • Use Explicit Type Conversion: When mixing data types, explicitly convert to the appropriate type.
  • Handle Edge Cases: Check for division by zero and other potential errors.

Practice Exercises

Test your understanding of PHP arithmetic operators with these exercises:

Exercise 1: Shopping Cart Calculator

Create a PHP script that calculates the total cost of items in a shopping cart, including tax and shipping.

<?php
/*
 * 1. Create variables for item prices
 * 2. Calculate subtotal
 * 3. Add 8% tax
 * 4. Add shipping ($5 for orders under $50, free for orders $50 or more)
 * 5. Output the itemized receipt
 */
?>

Exercise 2: Temperature Converter

Create a PHP script that converts temperatures between Fahrenheit and Celsius.

<?php
/*
 * Formula: F = (C × 9/5) + 32
 * Formula: C = (F - 32) × 5/9
 * 
 * 1. Convert 32°F to Celsius
 * 2. Convert 0°C to Fahrenheit
 * 3. Convert 98.6°F to Celsius
 * 4. Convert 37°C to Fahrenheit
 */
?>

Exercise 3: Simple Interest Calculator

Create a PHP script that calculates simple interest for a loan or investment.

<?php
/*
 * Formula: Simple Interest = Principal × Rate × Time
 *
 * 1. Calculate interest for $1000 at 5% for 2 years
 * 2. Calculate the total amount (principal + interest)
 * 3. Format the output with dollar signs and two decimal places
 */
?>

Summary

In this session, we've explored PHP's arithmetic operators and their real-world applications:

  • Addition (+): Combines values, useful for totaling items, calculating sums.
  • Subtraction (-): Finds differences, useful for inventory management, discounts.
  • Multiplication (*): Multiplies values, useful for scaling, quantity pricing.
  • Division (/): Divides values, useful for calculating averages, rates.
  • Modulus (%): Finds remainders, useful for alternating patterns, pagination.
  • Exponentiation (**): Raises to powers, useful for compound interest, growth calculations.
  • Integer Division (intdiv()): Performs whole number division, useful for time conversions.

Understanding arithmetic operators is fundamental to PHP programming. These operators enable you to perform calculations, process data, and create dynamic applications. As you progress through this course, you'll build on this knowledge to develop more complex functionality in your PHP and WordPress projects.

Next Session Preview

In our next session, we'll dive into PHP Assignment Operators, which allow you to assign values to variables in various ways, including combined operations like addition and assignment (+=). We'll also explore comparison operators that help you make decisions in your code.

Additional Resources