Larry Ullman's Book Forums

# Incrementing Not Working

## Recommended Posts

I'm in Chapter 4 page 84.

The topic is "Incrementing the value of a variable". The instructions are to change \$taxrate = \$taxrate + 1; to \$taxrate = \$taxrate++;

I am not getting the right results and I can't figure out why. The code was working before I made this change, and this is the only thing I changed. I've pasted that part of the code with the results below.

I'm using Microsoft Visual Web Developer 2010 Express to write my code, and my browser is IE8: I also tried it in Firefox which was even stranger as you will see below the IE8 results.

Below them all I have pasted the entire code

Here is the original (working) code:

//Determine the tax rate

\$taxrate = \$tax/100;

\$taxrate = \$taxrate +1;

You have selected to purchase:

10 widget(s) at

\$100 price each plus a

\$5.00 shipping cost and a

10 % tax rate.

After your \$0 discount, the total cost is \$1,105.50

Divided over 10 monthly payments, that would be \$110.55 each

-------------------------------------------- NOW THE CHANGE TO \$taxrate++;

//Determine the tax rate

\$taxrate = \$tax/100;

\$taxrate = \$taxrate ++;

You have selected to purchase:

10 widget(s) at

\$100 price each plus a

\$5.00 shipping cost and a

10 % tax rate.

After your \$0 discount, the total cost is \$100.50

Divided over 10 monthly payments, that would be \$10.05 each

--------------------------------------------

I decided to try it in Firefox and this was my result:

You have selected to purchase:

10 widget(s) at

\$10 price each plus a

\$5.00 shipping cost and a

10 % tax rate.

After your \$10 discount, the total cost is \$9.50

Divided over 10 monthly payments, that would be \$0.95 each

-----

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" >

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Product Cost Calculator</title>

<style type="text/css" media="screen">

.number {font-weight:bold;}

</style>

<body>

<?php //Script 4.2 - handle_calc.php

/* This script takes values from calculator.html and performs total cost and monthly payment calculations. */

// Address error handling, if you want.

ini_set ('display_errors', 1);

// Get the values from the \$_POST array:

\$price = \$_POST['price'];

\$quantity = \$_POST['quantity'];

\$discount = \$_POST['discount'];

\$tax = \$_POST['tax'];

\$shipping = \$_POST['shipping'];

\$payments = \$_POST['payments'];

// Calculate the total:

\$total = \$price * \$quantity;

\$total = \$total + \$shipping;

\$total = \$total - \$discount;

//Determine the tax rate

\$taxrate = \$tax/100;

\$taxrate = \$taxrate ++;

//Factor in the tax rate:

\$total = \$total * \$taxrate;

// Calculate the monthly payments:

\$monthly =\$total / \$payments;

//Apply number formatting

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

\$monthly = number_format (\$monthly, 2);

// Print the results

print "<div><p> You have selected to purchase: <br />

<span class=\"number\">\$quantity</span> widget(s) at <br />

\$<span class=\"number\">\$price </span> price each plus a <br />

\$<span class=\"number\">\$shipping</span> shipping cost and a<br />

<span class=\"number\">\$tax</span> % tax rate. <br />

After your \$<span class=\"number\">\$discount</span> discount, the total cost is

\$<span class=\"number\">\$total</span><br />

Divided over <span class=\"number\">\$payments</span> monthly payments, that would be \$<span class=\"number\">\$monthly</span> each

</p>

</div>";

?>

</body>

</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Product Cost Calculator</title>

<body><!-- Script 4.1 - calculator.html -->

<!--CREATE FORM -->

<div><p>Fill out this form to calculate the total cost:</p>

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

<p>Price: <input type="text" name="price" size="5"</p>

<p>Quantity: <input type="text" name="quantity" size="5"</p>

<p>Discount: <input type="text" name="discount" size="5"</p>

<p>Tax: <input type="text" name="tax" size="3" /> (%)</p>

<!-- SELECT SHIPPING -->

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

<!--SELECT PAYMENT ARRANGEMENTS -->

<p>Number of payments to make:<input type="text" name="payments" size="3" />

</p>

<input type="submit" name="submit" value="Calculate!" />

</form>

</div>

</body>

</html>

##### Share on other sites

I hate when I find the answer two seconds after I finally break down and ask. I'd struggled with this for a couple of hours and after I posted to this forum I decided to move to the next section of the chapter. That's when I remembered about the scripts. I pulled up the relevent script and almost immediately saw what I'd done wrong. Thanks anyway!!

##### Share on other sites

Happens to us all. Just to confirm, was the problem the space before the ++ or that the code should actually just be:

\$taxrate++;

without assignment?

##### Share on other sites

When I saw that it should just be taxrate++; I changed it to that and it worked. However I just pulled up the file and saw that I'd typed in as taxrate ++; so I'm glad you asked about the space because I hadn't noticed it and I now I can correct that too.

Larry, since you replied to this, I'd just like to thank you for this book! I tried an online "Introduction to PHP" class offered by a local technical college, and spent nearly all of my time completely confused by the teacher's instructions, explanations, and answers. Thank you for breaking this down into understandable bite-sized pieces that I can grasp even though the only 'programming' experience I've had was back in the dark ages of DOS and dBase.

I really can't thank you enough.

Happens to us all. Just to confirm, was the problem the space before the ++ or that the code should actually just be:

\$taxrate++;

without assignment?

##### Share on other sites

Thank you very much for the nice words. I'm glad you like what I do!