-2

I don't understand why my function getDisplayProductsNewPriceDiscountByQuantity return 0 then $new_discount_price return a price like 600 for example

  public function getProductsNewPriceByDiscountByQuantity($id, $qty, $products_price) {
....
      for ($i; $i > 0; $i--) {
        if($qty > $discount_quantity[$i]) {
          $new_discount_price  = ($products_price - ($products_price * ($discount_customer[$i] / 100))) * $qty;

          $this->displayDiscountPrice = $new_discount_price;
          break;
        }
      }

      return $new_discount_price; 
    }


    public function getDisplayProductsNewPriceDiscountByQuantity() {
      print_r('-------' . $this->displayDiscountPrice);
      return $this->displayDiscountPrice;
    }
kurama
  • 677
  • 3
  • 8
  • 16
  • You´ve posted the wrong function and the question is not very clear (for me) – derdida Sep 29 '16 at 14:34
  • 2
    `for ($i;` where are you setting `$i` – RiggsFolly Sep 29 '16 at 14:34
  • Ok the obvious question! Have you called `getProductsNewPriceByDiscountByQuantity()` or some other method that will set a value into `$this->displayDiscountPrice` BEFORE calling `getDisplayProductsNewPriceDiscountByQuantity()` – RiggsFolly Sep 29 '16 at 14:45
  • no $this->displayDiscountPrice take the value of $new_discount_price and $new_discount_price work fine – kurama Sep 29 '16 at 14:58

2 Answers2

0

Not sure what you're going for here, but the 2 functions have a slightly different name, and the $this->displayDiscountPrice is only set in the top one getProductsNewPriceByDiscountByQuantity. So if you call getDisplayProductsNewPriceDiscountByQuantity without calling the other function first the $this->displayDiscountPrice will be 0

Pim Broens
  • 702
  • 1
  • 5
  • 16
0

A highly recommended topic to review is the variables scope in general. This is a great topic, before going deep into OOP and properties scope.

Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?

Community
  • 1
  • 1
Anass
  • 59
  • 2
  • 8