Publishers of technology books, eBooks, and videos for creative people

• Print

Learn how to work specifically with number variables—both integers (whole numbers) and floating-point numbers (aka floats or decimals) in PHP.

This chapter is from the book

### This chapter is from the book 

Chapter 2, “Variables,” briefly discussed the various types of variables, how to assign values to them, and how they’re generally used. In this chapter, you’ll work specifically with number variables—both integers (whole numbers) and floating-point numbers (aka floats or decimals).

You’ll begin by creating an HTML form that will be used to generate number variables. Then you’ll learn how to perform basic arithmetic, how to format numbers, and how to cope with operator precedence. The last two sections of this chapter cover incrementing and decrementing numbers, plus generating random numbers. Throughout the chapter, you’ll also learn about other useful number-related PHP functions.

## Creating the Form

Most of the PHP examples in this chapter will perform various calculations based on an e-commerce premise. A form will take price, quantity, discount amount, tax rate, and shipping cost , and the PHP script that handles the form will return a total cost. That cost will also be broken down by the number of payments the user wants to make in order to generate a monthly cost value . Click to view larger image This form takes numbers from the user and sends them to a PHP page. Click to view larger image The PHP script performs a series of calculations on the submitted data and outputs the results. The results will look like this by the end of the chapter.

To start, let’s create an HTML page that allows the user to enter the values.

To create the HTML form:

1. Begin a new HTML document in your text editor or IDE, to be named calculator.html (Script 4.1):

```<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Product Cost Calculator</title>
<body><!-- Script 4.1 - calculator.html -->
<div><p>Fill out this form to calculate the total cost:</p>```

Script 4.1 This basic HTML form will provide the numbers for various mathematical calculations over multiple PHP scripts.

```1   <!doctype html>
2   <html lang="en">
4      <meta charset="utf-8">
5      <title>Product Cost Calculator</title>
7   <body><!-- Script 4.1 - calculator.html
-->
8   <div><p>Fill out this form to calculate
the total cost:</p>
9
10  <form action="handle_calc.php"
method="post">
11
12  <p>Price: <input type="text"
name="price" size="5"></p>
13
14  <p>Quantity: <input type="number"
name="quantity" size="5" min="1"
value="1"></p>
15
16  <p>Discount: <input type="text"
name="discount" size="5"></p>
17
18  <p>Tax: <input type="text" name="tax"
size="5"> (%)</p>
19
20  <p>Shipping method: <select
name="shipping">
option>
22  <option value="8.95">Put a move on it.</
option>
23  <option value="19.36">I need it
yesterday!</option>
24  </select></p>
25
26  <p>Number of payments to make: <input
type="number" name="payments" size="5"
min="1" value="1"></p>
27
28  <input type="submit" name="submit"
value="Calculate!">
29
30  </form>
31
32  </div>
33  </body>
34  </html>```
2. Create the initial form tag:

```<form action="handle_calc.php"
method="post">```

This form tag begins the HTML form. Its action attribute indicates that the form data will be submitted to a page named handle_calc.php. The tag’s method attribute tells the page to use POST to send the data. See Chapter 3, “HTML Forms and PHP,” for more details on choosing a method.

3. Create the inputs for the price, quantity, discount, and tax:

```<p>Price: <input type="text" name="price" size="5"></p>
<p>Quantity: <input type="number" name="quantity"
size="5" min="1" value="1"></p>
<p>Discount: <input type="text" name="discount" size="5"></p>
<p>Tax: <input type="text" name="tax" size="5"> (%)</p>```

Although HTML5 does have a number input type, it’s not always the right solution because it’s more naturally suited to taking integer values. For that reason, the quantity input will be a number type, whereas the others will be text.

To guide the user, a parenthetical indicates that the tax should be entered as a percent.

Remember that the names used for the inputs should correspond to valid PHP variable names: Use letters, numbers, and the underscore only; don’t start with a number; and so forth.

4. Add a field in which the user can select a shipping method:

```<p>Shipping method: <select name="shipping">
<option value="8.95">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select></p>```

The shipping selection is made using a drop-down menu. The value of the selected option is the cost for that option. If the user selects, for example, the Put a move on it. option, the value of \$_POST['shipping'] in handle_calc.php will be 8.95.

5. Complete the HTML form:

```<p>Number of payments to make: <input type="number" name="payments" size="5" min="1" value="1"></p>
<input type="submit" name="submit" value="Calculate!">
</form>```

The final two input types take a number for how many payments are required and then create a submit button (labeled Calculate!). The closing form tag marks the end of the form section of the page.

6. Complete the HTML page:

```</div>
</body>
</html>```
7. Save the script as calculator.html, and view it in your browser.

Because this is an HTML page, you can view it directly in a browser.