PHP – Lesson 04: Arithmetic Operators, Integers, and Floats

PHP Calulations

Arithmetic Operators

There are seven essential arithmetic operators:

  • + = addition
  • - = subtraction
  • * = multiplication
  • / = division
  • % = modulo division
  • ++ = increment (default adds 1)
  • -- = decrement (default subtracts 1)

If $x = 10,  $y = 20, and $z = 4, the following are true:

  • $x + $y  … is equal to 30
  • $y - $x … is equal to 10
  • $x * $y … is equal to 200
  • $y / $x … is equal to 2
  • $x % $z  … is equal to 2 (*SEE MODULO NOTE BELOW)
  • $x++  … is equal to 11  (*SEE INCREMENTS NOTE BELOW!)
  • $y--  … is equal to 19 (*SEE INCREMENTS NOTE BELOW!)

NOTE on Modulo: The modulo operator returns the remainder of a division formula. This is especially useful when you are trying to determine if something is even or odd; divide by “2” and the remainder will either be “0” or “1”.

NOTE on Increments: In the examples above for ++ and --, you’ll see that the variable comes BEFORE the increment symbols. Because of the way that PHP’s core increment functions are written, it literally sees $y++ or $y-- as a command where $y is announced and then incremented. This is confusing to beginners because when you echo the increment in the same line that it is declared, it seems like it is not changing from the result of the echo. For instance:

echo $y++;
/*This returns the original value of "20" because it echos the original value first ... and then adds the increment! */

To get a returned value of “21” to the screen, you would need to do this:

$y++;
echo $y;

An alternate way to use increment operators is to announce the increment before the variable, as in ++$y. This prepares the arithmetic operator to handle the variable and process it BEFORE it completes the echo command on the variable. So the following command would return the value of  “21” on the fly:

echo ++$y;


 

Determining the order of calculations

PHP follows standard math conventions when determining the order in which to do calculations:

  • Formulas in parentheses () are calculated first
  • Formulas containing multiplication, division, or modulus are second in line (* / %)
  • Formulas containing addition and subtraction are performed last (+ -)

Combining calculations and assignment

There is a shorthand way of performing a calculation on a variable and assigning a the result to that same variable. You can use addition, subtraction, multiplication, division, and modulo with this type of assignment. An addition example is:

  • $a += $b is the same as $a = $a + $b

A real example, where $a = 6 and $b = 4 at the start of the script:

  • $a += $b returns “10” because you are reassigning $a to equal itself plus 4.

Integers

So, in our Data Types overview lecture, we discovered that PHP will change data types for us on the fly. As a reminder, there are two basic kinds of numbers: Integers and floats. Let’s distinguish the first kind from the latter.

An integer is a whole number (no fractions or decimals). Some examples would be:

  • 1
  • 30
  • -30
  • 2495006
  • -1
  • 0

You can use all of the standard arithmetic operators on integers. Just remember that, as we discussed before, if you combine data types and type cast a result into integer form, you will lose any fractional values you might have had from a floating point variable in the equation.

Floats

A “float” is short for a “floating point number.” It simply means that this data type can store fractional values in decimal form. Here are some examples of floats:

1.5
848922.677777
0.69
0.00004
-4.2523
-8.2

There are some unique functions that are specifically made to use with floats. Let’s look right now at three basic and useful functions we can use with floating point numbers:

  • round(value, parameter)
    • Traditional rounding: half-up or half-down.
  • ceil()
    • Always rounds up (to the “ceiling”)
  • floor()
    • Always rounds down (to the “floor”)

Let’s look at some examples. Let’s say that we start off with a floating point variable equal to 4.6.

$floaty = 4.6288;

Here’s how our value would fare in the following functions:

echo round($floaty, 3); // result would be "4.629", round to 3rd decimal point

echo round($floaty, 2); // result would be "4.63", round to 2nd decimal point

echo round($floaty,1); // result would be 4.6", round to 1st decimal point

echo round($floaty,0); // result would be 5", round 0 decimal points

echo ceil($floaty); // result would also be "5"

echo floor($floaty); // result would be "4"

 

For a working example file with comments, download the floats.php testing file and load it into your web serving development folder.

 

Video Demos of Above Lecture

Arithmetic in PHP (15:00)

For better quality, select 720p from the gear icon in the lower right video frame.
For rapid initial loading, select 360p or lower from the gear icon in the lower right video frame. You can switch it to higher resolution shortly after several seconds of loading.

Arithmetic Functions in PHP (8:27)

For better quality, select 720p from the gear icon in the lower right video frame.
For rapid initial loading, select 360p or lower from the gear icon in the lower right video frame. You can switch it to higher resolution shortly after several seconds of loading.