canzo Posted April 2, 2006 Share Posted April 2, 2006 Hello,I'm kind of new to programming in PHP. I'm trying to do a retirement calculator and I have it basicly completed. But I keep getting warnings about division by zero.I tried to get this to work by making If statements so that if the certain optional variable is 0 then a formula without that variable will be executed. I'm getting very confused. The goal of the script is to get $cf, $s, $fv, $ar, and $da; which is then echoed in an html template. If you can help, I would greatly appreciate it! Division by zero errors happen on lines 93, 94, 94, 103, 141, 151, 151.Below is the entire snippit of code minus the html template.[code]<? if (($_POST[email] == "") || ($_POST[contact] == "")) { header('location: rrsp.html'); exit;}//Determine income frequency$e = 1; if ($_post[incomefreq] = "Annually") { $e = 1; } if ($_post[incomefreq] = "Semi-Annually") { $e = 2; } if ($_post[incomefreq] = "Quarterly") { $e = 4; } if ($_post[incomefreq] = "Monthly") { $e = 12; } if ($_post[incomefreq] = "Weekly") { $e = 52; } if ($_post[incomefreq] = "Bi-Weekly") { $e = 26; } //determine deposit frequency if ($_post[depositfreq] = "Annually") { $df = 1; } if ($_post[depositfreq] = "Semi-Annually") { $df = 2; } if ($_post[depositfreq] = "Quarterly") { $df = 4; } if ($_post[depositfreq] = "Monthly") { $df = 12; } if ($_post[depositfreq] = "Weekly") { $df = 52; } if ($_post[depositfreq] = "Bi-Weekly") { $df = 26; } //Put all Input POSTS into variablesif ($_POST[investrate] = 0) { $r = 0; } else { $r = ($_POST[investrate] / 100);}if ($_POST[inflation] = 0) { $i = 0; } else { $i = ($_POST[inflation] / 100); }$c = $_POST[income];$y = $_POST[yearsto];$f = $_POST[inflate];$yl = $_POST[tolast];$current = $_POST[current];$fd = $_POST[depositinflate];//Part 1. The income required in future dollars $cf = $c * (1 + $i)^$y;//where: //cf is the Income required with inflation//c is the income required in todays dollars//i s the inflation rate//y is the years to retirement //Part 2. The savings needed at retirement if ($f = "yes") { if ($r = $i) { $s = $cf * $yl * $e; } if ($r = 0) { $s = $cf * $e * ((1 + $i)^$yl - 1)/$i; } if ($r >= 0) { $e = 1; $a = (1 - ((1 + $r)/$e)^(-e) ) / ($r / $e); $b = (1 - ((1 + $i) / ((1 + $r / $e)^$e))^$yl ) / (1 - (1 + $i) / ((1 + $r / $e)^$e)); $s = $cf * (1 * $r / $e) * $a / $b; } } if ($f = "no") { if ($r = 0) { $s = $cf * $yl * $e; } else { $s = $cf * ((1 - (1 + $r / $e)^(-$yl * $e)) / ($r / $e)) * (1 + $r / $e); } }//Where://f is if income increases with inflation or not//s is the total savings at retirement//cf is the income required with inflation from Part 1.//yl is the years funds are to last//i is the inflation rate//r is the investment rate //e is the income frequency, such as annual is 1, weekly is 52.//Part 3. Inflated value of registered assets $fv = $current * (1+ $r)^($y);//Where://fv is the future value of current savings//r is the investment rate//y is the years to retirement//Part 4. Net Amount Required $ar = $s - $fv;//Where://ar is the net amount required to reach goal//s is the savings at retirement from Part 2//fv is the future valule of current savings from Part 3//Part 5. Amount of each deposit $rdf = $r / $df; $kdf = (1 + $rdf)^$df / (1 + $i); if ($fd = "yes") { if ($kdf = 1) { if ($rdf = 0) { $da = $ar / ($y * $df * (1 + $i)^($y)); } else { $da = $ar / ($y * (1 + $i)^($y) * (1 + $rdf) * (1 - (1 + $rdf)^(-$df) ) / $rdf); } } else { $da = $ar / (((($kdf)^($y) - 1) / ( $kdf - 1 )) * $kdf * (1 + $i)^($y) * (1 + $$rdf) * (1 - (1 + $rdf)^(-$df) ) / $rdf); } } if ($fd = "no") { if ($rdf = 0){ $da = $ar / ($y * $df); } else { $da = $ar / ((((1 + $rdf)^($y * $df) - 1) / $rdf) * (1 + $rdf)); } } //Where://fd is if the deposits increase with inflation or not.//i is the inflation rate//r is the investment rate//da is the deposit amount//ar is the net amount required from Part 4.//y is the years to retirement//rdf is the rate associated with deposit frequency rdf = r / df//kdf = (1+rdf)^df / (1+i)//df is the deposit frequemcy such as annually is 1, weekly is 52.?><html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>canzo</title><style fprolloverstyle>A:hover {color: #71552B}</style></head><body style="font-family: Verdana; font-size: 10pt; color: #000000" link="#005BA6" vlink="#CC6600" alink="#71552B" bgcolor="#FFFFFF"> <table border="0" width="100%" id="table7" cellspacing="0" cellpadding="0"> <tr> <td>Thank you for completing the RRSP Calculator with canzo! You have been e-mailed the results to <$email></td> </tr> <tr> <td>Income Required in Future Dollars = <? echo "$cf"; ?></td> </tr> <tr> <td>The Savings Needed at Retirement = <? echo "$s"; ?></td> </tr> <tr> <td>Inflated Value of Registered Assets = <? echo "$fv"; ?> </td> </tr> <tr> <td height="18">Net Amount Required = <? echo "$ar"; ?></td> </tr> <tr> <td height="12">Amount of Each Deposit <? echo "$dfpost"; ?> = <? echo "$da"; ?></td> </tr> <tr> <td height="11"><br> If you have any questions, please contact us!</td> </tr> </table></body></html>[/code]Thanks,Mark Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 2, 2006 Share Posted April 2, 2006 In your "if" statements you need to use the double equal sign "==" for the comparison. You are using a single equal sign "=" which does an assignment, which succeeds so you're "if" statement takes the success path -- not what you intended.You can get rid of the first two "if" statement blocks by using an array:[code]<?php$freq = array('Annually'=>1, 'Semi-Annually'=>2, 'Quarterly'=>4, 'Monthly'=>12, 'Bi-Weekly'=>26, 'Weekly'=>52);$e = 1;$e = $freq[$_POST['incomefreq']];$df = $freq[$_POST['depositfreq']];?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
canzo Posted April 2, 2006 Author Share Posted April 2, 2006 WOW! You're very fast. Thank you so much for your help! :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.