# 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:

• - (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:

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:

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. ## 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.

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.

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.

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.

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:

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)