Login Register


Possibly the weirdest bug fix. filter_list
Author
Message
Possibly the weirdest bug fix. #1
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.

Reply

RE: Possibly the weirdest bug fix. #2
(06-03-2014, 09:21 AM)Koala Wrote: 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.

Moral of the story; don't question the power of the almighty potato.
[Image: BXqGARG.png]

Reply

RE: Possibly the weirdest bug fix. #3
(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!
(This post was last modified: 06-03-2014, 09:40 AM by cr33pyguy.)
[Image: TeusoI9.png]
BACK UNDER YOUR BEDS
TRY TO GET A GOOD NIGHT'S SLEEP NOW


Reply

RE: Possibly the weirdest bug fix. #4
(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.

Reply

RE: Possibly the weirdest bug fix. #5
(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() ?
(This post was last modified: 06-03-2014, 10:38 AM by cr33pyguy.)
[Image: TeusoI9.png]
BACK UNDER YOUR BEDS
TRY TO GET A GOOD NIGHT'S SLEEP NOW


Reply

RE: Possibly the weirdest bug fix. #6
(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.

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() ?

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.

Reply

RE: Possibly the weirdest bug fix. #7
This is interesting. Someone else mind testing this out?

Reply

RE: Possibly the weirdest bug fix. #8
That is especially fucking weird. Good catch.
[Image: Ov15OiO.png]

Reply

RE: Possibly the weirdest bug fix. #9
(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.

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.

Okay, so it's practically saying $potato is it's value in the array $cart?
[Image: TeusoI9.png]
BACK UNDER YOUR BEDS
TRY TO GET A GOOD NIGHT'S SLEEP NOW


Reply

RE: Possibly the weirdest bug fix. #10
(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.

Reply







Users browsing this thread: