Functions

Hello everyone,

I just started learning PHP about two months ago and I’m currently taking an online course and I’m having trouble with an assignment when I have to call a function multiple times. I have three products which I’m trying to add together for a grand total. When I call the function twice it works perfect. If I call it a third time it’s only outputting the third product.

I’ve been trying to figure this out for three days if anyone can help me I would really appreciate it, and if someone could also explain why this function is working this way as well.

Thank you,

Charlie

//Here is my code

Checkout

Below is a summary of the products you wish to purchase, along with totals:

<?php #tax rate is constant $tax = 0.08; $total_price = 0; $total_tax = 0; $total_shipping = 0; $grand_total = 0; function productTotals($price, $tax, $shipping) { $total_price += $price; $total_tax += $tax * $price; $total_shipping += $shipping * $price; $grand_total = ($total_price + $total_tax + $total_shipping); return $grand_total; } ?>
    <?

    $product = “Candle Holder”;
    $price = 12.95;
    $shipping = “”; //free shipping
    echo “

  • ”.$product.": $".$price;

    productTotals($price, $tax, $shipping);
    $grand_total = productTotals($price, $tax, $shipping);

    $product = “Coffee Table”;
    $price = 99.50;
    $shipping = 0.10; //shipping as a percentage of price
    echo “

  • ”.$product.": $".$price;

    productTotals($price, $tax, $shipping);
    $grand_total = productTotals($price, $tax, $shipping);

    $product = “Lamp”;
    $price = 42.99;
    $shipping = 0.10; //shipping as a percentage of price
    echo “

  • ”.$product.": $".$price;

    productTotals($price, $tax, $shipping);
    $grand_total = productTotals($price, $tax, $shipping);

    ?>



Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?> .

The first thing you should know about functions is that variables used outside a function are not by default available inside the function (and vice versa). You’re using $total_price, etc. inside the function after declaring them above it, but they will be different variables. It is possible to fix this using the global keyword, but that’s not the best way to handle this issue.

I, and I hope any other self-respecting PHP programmer, would recommend you to use a class:

[code]<?php

class Basket {
private $tax = 0.08;
private $total_price = 0;
private $total_tax = 0;
private $total_shipping = 0;
private $grand_total = 0;

public __construct() {}

public function addProduct($price, $shipping) {
$this->total_price += $price;
$this->total_tax += $this->tax * $price;
$this->total_shipping += $shipping * $price;
$this->grand_total = $price * (1 + $this->tax + $shipping);
}

public function getGrandTotal() { return $this->grand_total; }
}

$basket = new Basket();
$basket->addProduct(12.95, 0); // candle holder
$basket->addProduct(99.50, 0.1); // coffee table
$basket->addProduct(42.99, 0.1); // lamp

echo $basket->getGrandTotal();

?>
[/code]

Naturally it would be good practice to expand on this example to add the description of the product (coffe table, lamp, etc) and to make an extra method called removeProduct(). Good luck ;)

Thank you so much.

Sponsor our Newsletter | Privacy Policy | Terms of Service