p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 (http://p2p.wrox.com/forumdisplay.php?f=33)
-   -   CH5 pg 150 While Loops (http://p2p.wrox.com/showthread.php?t=5708)

wadesmart November 3rd, 2003 03:28 PM

CH5 pg 150 While Loops
 
I am getting errors:
"PHP Fatal error: Maximum execution time of 30 seconds exceeded in C:\Documents and Settings\Administrator\My Documents\My Work\PHP\Beg_PHP4\CH5\loan2.php on line 25, referer: http://localhost/PHP/Beg_PHP4/CH5/loan2.htm"

Actually, it is lines 21 through 30. On the following code, the <?php is line 4.

<?php
        $Duration = 0;
        switch ($_POST["loan"])
        {
            case 1000: $Interest = 5;
            break;

            case 5000: $Interest = 6.5;
            break;

            case 1000: $Interest = 8;
            break;

            default:
            echo "You didnt chose a loan package.";
            exit;
        }

        while ($_POST["loan"] >= 0 )
        {
            $Duration = $Duration + 1 ;
            $Monthly = $_POST["month"] - ($_POST["loan"] * $Interest / 100 ) ;
            if ($Monthly <= 0)
            {
                echo "You need larger monthly payments to pay off your loan." ;
            }
        $NLoan = $_POST["loan"] + $Monthly;
        }

            echo "This would take you $Duration months to pay this off at the interest rate of $Interest percent." ;
    ?>



richard.york November 3rd, 2003 04:29 PM

The reason that you are getting that error is because you have an infinite loop. You're saying execute this code while $_POST["loan"] is greater than or equal to zero, and since you are doing nothing with the $_POST["loan"] variable to decrease its value, that condition isn't ever being met.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

richard.york November 3rd, 2003 04:32 PM

Or rather, that condition is *always* being met! And the loop never terminates.

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

wadesmart November 3rd, 2003 07:28 PM

Ok. will this work.

while ($_POST["loan"] > 0 )
        {
            $Duration = $Duration + 1 ;
            $Monthly = $_POST["month"] - ($_POST["loan"] * $Interest / 100 ) ;
            if ($Monthly <= 0)
            {
                echo "You need larger monthly payments to pay off your loan." ;
            }
        $_POST["loan"] = $_POST["loan"] + $Monthly;
        }




richard.york November 3rd, 2003 07:37 PM

Well no, you're still increasing the value of $_POST["loan"]

In plain English you're saying:

While $_POST["loan"] is greater than zero *always* execute this code.
Meanwhile you are increasing, not decreasing the value of $_POST["loan"], so it is *always* going to be greater than zero and therefore will make an infinite loop.

I'm going to have a look at your code and get back to you in a minute.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

richard.york November 3rd, 2003 07:43 PM

The skinny of it is I think you want to be subtracting, not adding the $Monthly variable.

$_POST["loan"] = $_POST["loan"] - $Monthly;

Perhaps for clarity this would be easier:

$Principle = $_POST["loan"];

First you calculate accrued interest on the $Principle, then you would be subtracting your $Monthly payment from the $Principle to get the $Duration.

Make sense?
: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

wadesmart November 3rd, 2003 07:50 PM

So,

<?php
        $Principle = $_POST["loan"];
        $Duration = 0;
        switch ($Principle)
        {
            case 1000: $Interest = 5;
            break;

            case 5000: $Interest = 6.5;
            break;

            case 1000: $Interest = 8;
            break;

            default:
            echo "You didnt chose a loan package.";
            exit;
        }

        while ($Principle > 0 )
        {
            $Duration = $Duration + 1 ;
            $Monthly = $_POST["month"] - ($_POST["loan"] * $Interest / 100 ) ;
            if ($Monthly <= 0)
            {
                echo "You need larger monthly payments to pay off your loan." ;
            }
        $Principle = $Principle + $Monthly;
        }

            echo "This would take you $Duration months to pay this off at the interest rate of $Interest percent." ;
    ?>



richard.york November 3rd, 2003 08:05 PM

You still need to subtract the $Monthly from the $Principle. I assume $Monthly is the calculated monthly payment amount.

On this line:
$Principle = $Principle - $Monthly;

I'm looking at the book now and that appears to be the only error.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

nikolai November 4th, 2003 01:10 PM

Okay, Wade -- you seriously need to step back and just look at your code. Make up some numbers, grab a pencil and paper (or use a whiteboard, as I prefer), and trace through your code. Take a look for yourself what's happening. Come up with some sample values for your variables, write them down, and keep track of each variable value as your code runs.

When I read the code, I wonder why you have a while() loop at all. I mean, Rich is correct in diagnosing your problem -- an infinite loop... but why are you looping at all? What is it your loop is meant to do?


The way I read it, a user submits a loan amount and a monthly payment, and you calculate how long it'll take to pay off that loan at that monthly rate.

The reason you're using a loop is because each iteration of the loop represents a month's payment. After each iteration, the loan balance *should* go down -- that implies that you WILL approach a zero balance sooner or later.

If the balance does NOT change, or worse, it rises, then you've got a problem -- your monthly payment either matches or is less than the interest charge for that month. Your script does test for this, but it tests for it within the body of the loop and doesn't exit the loop if the test fails, which will also cause an infinite loop.

Here's my suggested rewrite of your code:

<?php

switch ($_POST["loan"])
{
    case '1000': // $_POST['loan'] is a STRING, not an integer.
    {
        $interest = .05; // 5%
    }
    break;

    case '5000':
    {
        $interest = .065; //6.5%
    }
    break;

    case '10000':
    {
        $interest = .08; // 8%
    }
    break;

    default:
    {
        echo "You didn't choose a loan package.\n";
        exit;
    }
}

$balance = round((float)$_POST['loan'], 2); // convert to float
$monthly = round((float)$_POST['month'], 2); // ditto


// Before we start the loop, test whether the monthly
// payment is enough to cover the interest:
$first_month_interest = round(($balance * $interest), 2);

if (($first_month_interest) >= $montly)
{
    echo "You will never pay off the loan. "
       . "Your monthly payment is too low.\n";
    exit;
}

for ($duration = 0; $balance > 0; ++$duration)
{
    $balance = round($balance * (1 + $interest) - $monthly, 2);
}


echo "It would take {$duration} months to pay off this loan at ";
printf("%.1f", ($interest * 100)); // nicely formats float
echo "%.\n";

?>


I hope this version makes a little more sense...


Take care,

Nik
http://www.bigaction.org/

richard.york November 8th, 2003 04:04 PM

Quote:

quote:Originally posted by Nikolai
When I read the code, I wonder why you have a while() loop at all. I mean, Rich is correct in diagnosing your problem -- an infinite loop... but why are you looping at all? What is it your loop is meant to do?
Well as far as that goes this code is exactly what appears in the book. So that's not exactly Wade's fault. Yes it really is that screwy.

: )
Rich



:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::


All times are GMT -4. The time now is 04:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.