Skip to main content
March 23, 2026Noble Desktop Publishing Team/3 min read

Working with Numbers

Master PHP Mathematical Operations and Number Handling

Prerequisites

This tutorial assumes you have a local PHP development environment set up with either MAMP (Mac) or XAMPP (Windows) running on your system.

Topics Covered in This PHP & MySQL Tutorial:

Arithmetic Operators, Assignment Operators, Table of Arithmetic Operators, Table of Assignment Operators

Exercise Overview

Mathematical operations form the backbone of virtually every programming task you'll encounter in your development career. Whether you're calculating user metrics, processing financial data, or manipulating array indices, PHP's arithmetic capabilities are tools you'll reach for daily. This tutorial will guide you through PHP's mathematical operators with practical, hands-on examples that demonstrate real-world applications.

Tutorial Learning Path

1

Basic Arithmetic

Learn fundamental mathematical operations in PHP using standard arithmetic operators

2

Assignment Operators

Master shorthand operators for efficient variable manipulation and calculation

3

Hands-on Practice

Work with the math.php file to see operators in action with real examples

Arithmetic Operators

  1. In your code editor, open math.php from the phpclass folder.

  2. In between the <body> tags add the following bold code:

    <?php 
       $myVar = 2;
       $myVar = $myVar + 1;
       echo $myVar;
    ?>

    This code demonstrates basic variable assignment and arithmetic. We initialize $myVar with the value 2, perform an addition operation to increment it by 1, and display the result. This pattern—storing a value, modifying it, then outputting the result—is fundamental to data processing in PHP applications.

  3. Save the page and then in a browser navigate to:

    • Mac: localhost:8888/phpclass/math.php
    • Windows: localhost/phpclass/math.php

    The page should display:

    3

  4. Return to your code editor to explore a more efficient approach.

  5. PHP provides shorthand operators for common operations. Select the last two lines, then replace them with the following bold code:

    <?php 
       $myVar = 2;
       $newVar = $myVar++;
       echo '$myVar: '. $myVar;
    ?>

    The increment operator (++) increases $myVar by one in a single operation. Note that $myVar++ (post-increment) returns the original value before incrementing, while ++$myVar (pre-increment) returns the value after incrementing—a distinction that becomes crucial in complex expressions.

  6. Save the page, return to your browser, and reload math.php.

    You should now see:

    $myVar: 3

Now let's explore assignment operators, which provide even more flexibility for mathematical operations in PHP.

Code Examples from Tutorial

Basic Addition

Setting $myVar to 2, adding 1, and displaying the result of 3. This demonstrates fundamental variable manipulation in PHP.

Increment Operator

Using $myVar++ to increment by 1 automatically. The post-increment operator adds 1 after the current value is used.

Local Server Access

Mac users access files at localhost:8888/phpclass/math.php while Windows users use localhost/phpclass/math.php for testing PHP code.

PHP Arithmetic Operations

Basic Math Operations

Addition (+), subtraction (-), multiplication (*), and division (/) work exactly as expected in mathematical contexts.

Advanced Operations

Modulus (%) returns remainders, while increment (++) and decrement (--) operators modify values by one unit.

Operator Usage Frequency

Addition
6
Assignment
5
Increment
3
Multiplication
2
Division
2
Modulus
1

Assignment Operators

Assignment operators combine variable assignment with arithmetic operations, creating cleaner, more readable code. These operators are particularly valuable when working with counters, accumulators, or any scenario where you need to modify a variable's value based on its current state.

  1. Return to your code editor to implement an assignment operator.

  2. Replace the last two lines with the following bold code:

    $myVar = 2;
    $myVar += 5;
    echo $myVar;

    This demonstrates the addition assignment operator. We initialize $myVar with 2, then use += to add 5 to its current value in a single operation. The expression $myVar += 5 is functionally equivalent to $myVar = $myVar + 5, but offers improved readability and reduces the chance of typos in variable names.

  3. Save the file, refresh your browser, and reload math.php.

    The output should now display 7.

  4. Close the file—you've successfully implemented both arithmetic and assignment operators.

To solidify your understanding, here are comprehensive reference tables for PHP's mathematical operators that you'll find invaluable in your daily development work.

Assignment Operator vs Standard Syntax

FeatureShorthandLonghand Equivalent
Addition Assignment$myVar += 5$myVar = $myVar + 5
Result7 (when $myVar starts at 2)7 (when $myVar starts at 2)
Recommended: Assignment operators provide cleaner, more readable code for mathematical operations.

Assignment Operator Mastery

0/6
Tutorial Complete

You have successfully learned PHP arithmetic and assignment operators. The math.php file can now be closed as all exercises are finished.

Table of Arithmetic Operators

Operator Meaning Example
+ Addition $n + 1
- Subtraction $n - 5
* Multiplication $n * 20
/ Division $n / 3
% Modulus (remainder) $n % 6
++ Increment ++$n
-- Decrement --$n

Code Examples from Tutorial

Basic Addition

Setting $myVar to 2, adding 1, and displaying the result of 3. This demonstrates fundamental variable manipulation in PHP.

Increment Operator

Using $myVar++ to increment by 1 automatically. The post-increment operator adds 1 after the current value is used.

Local Server Access

Mac users access files at localhost:8888/phpclass/math.php while Windows users use localhost/phpclass/math.php for testing PHP code.

PHP Arithmetic Operations

Basic Math Operations

Addition (+), subtraction (-), multiplication (*), and division (/) work exactly as expected in mathematical contexts.

Advanced Operations

Modulus (%) returns remainders, while increment (++) and decrement (--) operators modify values by one unit.

Operator Usage Frequency

Addition
6
Assignment
5
Increment
3
Multiplication
2
Division
2
Modulus
1

Table of Assignment Operators

Example Meaning Alternative Longhand Syntax
$n = 3 $n equals 3 $n = 3
$n += 3 $n equals $n plus 3 $n = $n + 3
$n -= 3 $n equals $n minus 3 $n = $n - 3
$n *= 3 $n equals $n times 3 $n = $n * 3
$n /= 3 $n equals $n divided by 3 $n = $n / 3
$n %= 3 $n equals the remainder of $n divided by 3 $n = $n % 3
$n .= 3 $n equals $n concatenated with 3 $n = $n . 3

Assignment Operator vs Standard Syntax

FeatureShorthandLonghand Equivalent
Addition Assignment$myVar += 5$myVar = $myVar + 5
Result7 (when $myVar starts at 2)7 (when $myVar starts at 2)
Recommended: Assignment operators provide cleaner, more readable code for mathematical operations.

Assignment Operator Mastery

0/6
Tutorial Complete

You have successfully learned PHP arithmetic and assignment operators. The math.php file can now be closed as all exercises are finished.

Key Takeaways

1PHP arithmetic operators (+, -, *, /, %, ++, --) handle mathematical calculations with familiar syntax
2Assignment operators (+=, -=, *=, /=, %=, .=) provide shorthand notation for common variable operations
3The increment operator (++) adds 1 to a variable value automatically
4Assignment operators like += are equivalent to longer syntax ($myVar = $myVar + value) but more concise
5Local PHP development requires different localhost URLs for Mac (port 8888) versus Windows systems
6Variables can be manipulated through direct assignment, arithmetic operations, or combination operators
7The modulus operator (%) returns the remainder of division operations for specialized calculations
8String concatenation uses the .= operator as shorthand for combining text values with variables

RELATED ARTICLES