jmalone Posted February 1, 2013 Share Posted February 1, 2013 The code below doesn't work. I believe the function is not executing, but I've been hunting and pecking for too long and need some input. Can anyone take time out to help with this? Best, J. <B>Checkout</B><br> Below is a summary of the products you wish to purchase, along with totals: <?php #tax rate is constant $price = 0; $tax = 0.08; $total_price = 0; $total_tax = 0; $shipping = 0; $total_shipping = 0; $grand_total = 0; ?><ul><? function tallyTotals($grand_total) { $total_price += $price; $total_tax += $tax * $price; $total_shipping += $shipping * $price; $grand_total = ($total_price + $total_tax + $total_shipping); } $product = "Candle Holder"; $price = 11.95; $shipping = 0; //free shipping echo "<li>".$product.": $".$price; tallyTotals($grand_total); $product = "Coffee Table"; $price = 99.50; $shipping = 0.10; //shipping as a percentage of price echo "<li>".$product.": $".$price; tallyTotals($grand_total); $product = "Floor Lamp"; $price = 44.99; $shipping = 0.10; //shipping as a percentage of price echo "<li>".$product.": $".$price; tallyTotals($grand_total); ?> </ul> <hr> <br> <B>Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?></B> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 1, 2013 Share Posted February 1, 2013 Go read the manual on variable scope. And "return". And turning on error reportingz Quote Link to comment Share on other sites More sharing options...
jcbones Posted February 1, 2013 Share Posted February 1, 2013 I created a new function to try and help you understand the concept. It is fully commented. Note that I left your function and calls in the script, but commented them out. I also added error_reporting to the script as well. You should really set error reporting in you php.ini though. <?php //turn on error_reporting error_reporting(-1); ini_set('display_errors',1); #tax rate is constant $price = 0; $tax = 0.08; $total_price = 0; $total_tax = 0; $shipping = 0; $total_shipping = 0; $grand_total = 0; ?><ul><?php // function tallyTotals($grand_total) { // $total_price += $price; // $total_tax += $tax * $price; // $total_shipping += $shipping * $price; // $grand_total = ($total_price + $total_tax + $total_shipping); // } ///////////////////Any variable created in a function, is only available to that function. //////////////////To get information out of a function, you must return the information. function totalPrice($price, $shipping, $percentage = FALSE) { //create new function with 3 arguments, first 2 MUST be supplied, the third is optional. if($percentage == TRUE) { //if the 3rd argument is changed to true, then run this block of code. return $price + ($price * $shipping); //and return the price + a percentage of the price for shipping charges. } return $price + $shipping; //if the shipping is not a percentage, then just add it to the price, and return the result. } $product = "Candle Holder"; $price = 11.95; $shipping = 0; //free shipping echo "<li>".$product.": $".$price; $grand_total += totalPrice($price,$shipping); //Send the arguments to the function, notice there is no 3rd argument supplied, the function will use the pre-defined FALSE. returned data will be stored in $grand_total. //tallyTotals($grand_total); $product = "Coffee Table"; $price = 99.50; $shipping = 0.10; //shipping as a percentage of price echo "<li>".$product.": $".$price; $grand_total += totalPrice($price,$shipping,TRUE); //Another call to totalPrice, this time with 3 arguments, since we need to change the shipping to a percentage by setting the 3rd argument to TRUE. //tallyTotals($grand_total); $product = "Floor Lamp"; $price = 44.99; $shipping = 0.10; //shipping as a percentage of price echo "<li>".$product.": $".$price; $grand_total += totalPrice($price,$shipping,TRUE); //same as above. //tallyTotals($grand_total); ?> </ul> <hr> <br> <B>Total (including tax and shipping): $<?php echo number_format($grand_total, 2); ?></B> Quote Link to comment Share on other sites More sharing options...
jmalone Posted February 2, 2013 Author Share Posted February 2, 2013 Thank you both for your help. I used jcbones code and was able to see what I was missing. I will definately read the manual ... again : ) ... to try and clear the fog. Thanks again, J. 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.