Intro to PHP Part 4, Numbers and math operators

In this fourth post in the PHP series I'll go over numbers and math operators. There are two types of numbers, integers, which are whole numbers, and floats which are decimal numbers. These numbers never have quotation marks around them, the way that strings always do. You can perform math using the following operators:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulos)
  • ++ (increment)
  • -- (decrement)

PHP also has a number of built in functions for working with numbers. For example, there is the round() function, which will round a number to the desired decimals. Here's an example:

1
2
3
$number = 3.141592;
$number = round($number, 2);
echo"$number";

The following code will output 3.14 rather than 3.141592 because the round() function is rounding to 2 decimal places. Another useful number function is number_format() which will add commas to group numbers by thousands. Here's an example:

1
2
3
$bigNumber = 100000000;
$bigNumber = number_format(bigNumber)
echo"$bigNumber";

This will output 100,000,000 complete with those two commas, which makes the number much easier to read.
Now to practice using math operators, let's make a new file called program3.php in which we'll perform some simple math operations that could apply to a retail store or ecomerce application. You'll notice that I use comments in this program, which are preceded by double slashes // This is simply to make the code more understandable for us programmers. The users will not be able to see any PHP comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    <?php
    //defining the variables
    $quantity = 30; //buying 30 items
    $price = 5.99;
    $tax_rate = .05; //5% tax rate

    //calculate total at checkout
    $subtotal = $quantity * $price;
    $total = $subtotal + ($subtotal * $tax_rate); //calculate total and add tax

    //format the total
    $total = number_format($total, 2); 

    //print the results
    echo"<p>You are buying $quantity items at a price of $price each, for a total of $$total";
    ?>

program1

Break it down!

This program is a fair bit longer than the other ones we've written together so far, so I'll break this one down into smaller pieces and explain each chunk in detail.

1
2
3
    $quantity = 30; //buying 30 items
    $price = 5.99;
    $tax_rate = .05; //5% tax rate

In the code above, I've defined three variables and hard coded their values in. Later on, I'll show how to use user input through an HTML form to generate these values instead, so that this program is more dynamic.


1
2
    $subtotal = $quantity * $price;
    $total = $subtotal + ($subtotal * $tax_rate); //calculate total and add tax

The above code is where the actual calculation takes place. The first line establishes the subtotal as being the number of items multiplied by the price of the items. The second line then adds the tax rate, by multiplying the tax rate by the subtotal, thereby finding the total cost.


1
    $total = number_format($total, 2); 

In the code above, I'm using the number_format() function that I mentioned earlier. This will round the output to two places after the decimal. If I hadn't used number_format() then the output would have been $188.685 instead of $188.69.


1
    echo"<p>You are buying $quantity items at a price of $price each, for a total of $$total";

This last step just outputs the result into a sentence, using the methods discussed in the last post. That's all there is to it! You can experiment with different values for the first three variables. For example:

1
2
3
    $quantity = 2340; 
    $price = 1319.45;
    $tax_rate = .05; //5% tax rate

Plugging these numbers in will result in a total of $3,241,888.65!
In the next post, we'll learn about user input and how to use them in your PHP programs to handle submitted values


Special thanks to Larry Ullman, whose textbooks helped me learn PHP programming (no affiliation)

Follow me!