![]() |
|
Possibly the weirdest bug fix. - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: Possibly the weirdest bug fix. (/Thread-Possibly-the-weirdest-bug-fix) Pages:
1
2
|
Possibly the weirdest bug fix. - Lain - 06-03-2014 So, I had a PHP exam where I had to code a shopping cart class with functions to add and to calculate the total cost of the items in the cart (pretty basic stuff), and I removed a useless line of code after $total = 0;, but then the total cost of the items started to return 0, for no apparent reason. Spoiler:PHP Code: <?php
class shopping_cart
{
var $cart;
function addItem($id, $name = "undefined", $price = 0, $quantity)
{
$this->cart[$id] = array('name' => $name, 'price' => $price, 'quantity' => $quantity);
}
function getTotal() {
$total = 0;
$potato = $this->cart; //random glitch returns 0 when this line is not here.
while ($temp = current($this->cart)) {
if (!is_array($temp)) {
$total += key($this->cart).': '.current($this->cart);
} else {
$cache2 = current($this->cart);
$total += $cache2["price"]*$cache2["quantity"];
}
next($this->cart);
}
echo $total;
}
function outputCart() {
while ($temp = current($this->cart)) {
if (!is_array($temp)) {
$data[] = key($this->cart).': '.current($this->cart);
} else {
$part2 = current($this->cart);
while ($temp2 = current($part2)) {
$data[] = key($part2).': '.current($part2);
next($part2);
}
}
next($this->cart);
}
echo $data = implode ('<br/>', $data);
}
}
$shoppingCart = new shopping_cart;
$shoppingCart->addItem( 1, "Bacon", 10.40, 3);
$shoppingCart->addItem( 2, "Chocolate", 4.40, 2);
$shoppingCart->outputCart();
echo "<br/>total price: ";
$shoppingCart->getTotal();
?>So adding this line after $total = 0, somehow fixed it; PHP Code: $potato = $this->cart; //random glitch returns 0 when this line is not here.
I don't even see how the above line of code does anything. RE: Possibly the weirdest bug fix. - Equinox - 06-03-2014 (06-03-2014, 09:21 AM)Koala Wrote: So adding this line after $total = 0, somehow fixed it; Moral of the story; don't question the power of the almighty potato. RE: Possibly the weirdest bug fix. - cr33pyguy - 06-03-2014 (06-03-2014, 09:26 AM)Duubz Wrote: Moral of the story; don't question the power of the almighty potato. DID HE? Someone hang him now! Apparently your groceries won't cost anything unless you buy potatoes! RE: Possibly the weirdest bug fix. - Lain - 06-03-2014 (06-03-2014, 09:26 AM)Duubz Wrote: Moral of the story; don't question the power of the almighty potato. (06-03-2014, 09:39 AM)cr33pyguy Wrote: Apparently your groceries won't cost anything unless you buy potatoes! Oh my. Clearly potatoes are trying to take over the world. RE: Possibly the weirdest bug fix. - cr33pyguy - 06-03-2014 (06-03-2014, 09:57 AM)Koala Wrote: Oh my. Clearly potatoes are trying to take over the world. They'll grow giant, and roll over you. And then they'll eat smashed humans with grass sauce. OT: I've done only a small amount of PHP, so excuse my confusion, but what exactly does the operator -> do? is it similar to Java's .add() ? RE: Possibly the weirdest bug fix. - Lain - 06-03-2014 (06-03-2014, 10:34 AM)cr33pyguy Wrote: They'll grow giant, and roll over you. And then they'll eat smashed humans with grass sauce. Well, $this is referring to the parent class, -> is used for referencing variables and functions within the class. For example, in the code above, I have the variable/array called $cart, I'm giving the variable $potato the value stored in the $cart variable. PHP Code: $potato = $this->cart;
For some better explanations, I suggest checking out their documentation. RE: Possibly the weirdest bug fix. - Eclipse - 06-03-2014 This is interesting. Someone else mind testing this out? RE: Possibly the weirdest bug fix. - Christ - 06-03-2014 That is especially fucking weird. Good catch. RE: Possibly the weirdest bug fix. - cr33pyguy - 06-04-2014 (06-03-2014, 12:25 PM)Koala Wrote: Well, $this is referring to the parent class, -> is used for referencing variables and functions within the class. Okay, so it's practically saying $potato is it's value in the array $cart? RE: Possibly the weirdest bug fix. - Lain - 06-04-2014 (06-04-2014, 08:54 AM)cr33pyguy Wrote: Okay, so it's practically saying $potato is it's value in the array $cart? Well, it's giving $potato, the value that $cart has. |