Skip to main content
Noble Desktop Publishing Team/3 min read

Working with Numbers

PHP Arithmetic & Assignment

Basic Operators

+ - * / for arithmetic; % for modulus; ** for exponent (PHP 5.6+).

Increment Operators

$x++ post-increments (returns then adds); ++$x pre-increments (adds then returns).

Compound Assignment

$x += 5 is shorthand for $x = $x + 5; same with -=, *=, /=, .= for strings.

String Concatenation Uses .

echo 'Result: ' . $myVar; — PHP uses dot, not + (which is for math only).

Build Back-End Skills at Noble Desktop

Noble Desktop's Full-Stack Web Development Certificate teaches modern back-end development — concepts that carry across PHP, Node.js, and Python.

Explore the function and application of arithmetic and assignment operators in PHP and MySQL, with practical exercises for hands-on learning.

Topics Covered in This PHP & MySQL Tutorial:

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

Exercise Overview

Math! Who doesn’t love it? OK, maybe not everyone loves math, but as programmers it’s something we end up doing quite a bit of. Here we’ll explore how PHP works with numbers.

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 makes a new variable called $myVar with the value of 2, adds 1 to it, and then displays the new value.

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

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

    The page should read:

    3

  4. Switch back to your code editor.

  5. We can also increment the variable by 1. Select the last two lines, then replace them with the following bold code:

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

    This increments the value of $myVar by one.

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

    It should now say:

    $myVar: 3

Assignment Operators

What if you wanted to increment the value of a variable directly and by a value other than one? You’d use what is called an Assignment Operator.

  1. Switch back to your code editor.

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

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

    This makes a variable called $myVar with the value of 2, adds 5 to it, and then displays the new value.

    $myVar += 5 is equivalent to $myVar = $myVar + 5

  3. Save the page, go to the browser, and reload math.php.

    The screen should now say 7.

  4. Close the file. We’re done with it.

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

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