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
Basic Arithmetic
Learn fundamental mathematical operations in PHP using standard arithmetic operators
Assignment Operators
Master shorthand operators for efficient variable manipulation and calculation
Hands-on Practice
Work with the math.php file to see operators in action with real examples
Arithmetic Operators
In your code editor, open math.php from the phpclass folder.
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
$myVarwith 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.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
Return to your code editor to explore a more efficient approach.
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$myVarby 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.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.
Mac users access files at localhost:8888/phpclass/math.php while Windows users use localhost/phpclass/math.php for testing PHP code.
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.
Return to your code editor to implement an assignment operator.
Replace the last two lines with the following bold code:
$myVar = 2; $myVar += 5; echo $myVar;This demonstrates the addition assignment operator. We initialize
$myVarwith 2, then use+=to add 5 to its current value in a single operation. The expression$myVar += 5is functionally equivalent to$myVar = $myVar + 5, but offers improved readability and reduces the chance of typos in variable names.Save the file, refresh your browser, and reload math.php.
The output should now display 7.
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
| Feature | Shorthand | Longhand Equivalent |
|---|---|---|
| Addition Assignment | $myVar += 5 | $myVar = $myVar + 5 |
| Result | 7 (when $myVar starts at 2) | 7 (when $myVar starts at 2) |
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.
Mac users access files at localhost:8888/phpclass/math.php while Windows users use localhost/phpclass/math.php for testing PHP code.
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
| Feature | Shorthand | Longhand Equivalent |
|---|---|---|
| Addition Assignment | $myVar += 5 | $myVar = $myVar + 5 |
| Result | 7 (when $myVar starts at 2) | 7 (when $myVar starts at 2) |