crossfuse Posted April 13, 2009 Share Posted April 13, 2009 I'm trying to make a function to add 6% tax to the total cost which the user can type in the cost of an item. The outcome is supposed to include the shipping as well (which I've done),but using variables isn't my forte. I understand the concept of them but here's what I got so far. http://cpsc.maconstate.edu/students/justin.ham/cost.html <?php print("<b>Thankyou!</b><br><br><br>"); $cost = $_POST["cost"]; $cost1 = $cost + 3; $cost2 = $cost + 4; $cost3 = $cost + 5; $cost4 = $cost + 6; if ($cost < 0) { print("Your total cost is invalid"); } elseif ($cost <= 25) { print("Your total cost,plus shipping, is $$cost1"); } elseif ($cost <= 50) { print("Your total cost,plus shipping, is $$cost2"); } elseif ($cost <= 75) { print("Your total cost,plus shipping, is $$cost3"); } elseif ($cost > 75) { print("Your total cost,plus shipping, is $$cost4"); } ?> do i insert a function before the 'if' statement and what exactly would i put? Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/ Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 If you want to add 6%, just multiply it by 1.06. Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808262 Share on other sites More sharing options...
crossfuse Posted April 13, 2009 Author Share Posted April 13, 2009 If you want to add 6%, just multiply it by 1.06. i would but i need to make it a function... Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808272 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 Ok then. Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808275 Share on other sites More sharing options...
crossfuse Posted April 13, 2009 Author Share Posted April 13, 2009 Ok then. how would i multiply 6% using a function? Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808279 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 function add_six_percent($number) { return $number * 1.06; } Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808282 Share on other sites More sharing options...
.josh Posted April 13, 2009 Share Posted April 13, 2009 echo "<b>Thankyou!</b><br><br><br>"; $cost = $_POST["cost"]; if ($cost < 0) { $msg = "Your total cost is invalid"; } else { $shipping = array(3 => 25, 4 => 50, 5 => 75, 6 => 76); foreach ($shipping as $k => $val) { if ($cost <= $val) { $cost = ($cost * 1.06) + $k; $msg = "Your total cost, plus shipping, is $$cost"; break; } // end if } // end foreach } // end if..else echo $msg; Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808283 Share on other sites More sharing options...
crossfuse Posted April 13, 2009 Author Share Posted April 13, 2009 function add_six_percent($number) { return $number * 1.06; } how would i make that work with my code? Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808950 Share on other sites More sharing options...
jackpf Posted April 13, 2009 Share Posted April 13, 2009 If you want to add six percent to a number, just do $number = 2; $number2 = add_six_percent($number); echo $number; //outputs 2 echo $number2; //outputs 2.12 Link to comment https://forums.phpfreaks.com/topic/153794-function-for-multiplying-total-cost/#findComment-808961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.