Working with Numbers

Free PHP & MySQL Tutorial

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

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.

Topics covered in this PHP & MySQL tutorial:

Arithmetic operators, Assignment operators, Table of arithmetic operators, Table of assignment operators

SQL Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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

How to Learn Full-Stack Web Development

Master full-stack web development with hands-on training. Build fully functional websites and applications using HTML, CSS, JavaScript, Python, and web developer tools.

Yelp Facebook LinkedIn YouTube Twitter Instagram