user3580417
user3580417

Reputation: 7

Function always returns the value from the first condition

Total newbie to PHP with problem. Here is function that calculate if value is between ex. 0,00 and 1.500,00 then discount number is 8

function wpsc_cart_discount_number(){
    global $wpsc_cart;
    if   ($total >= 0000 && $total < 1500) {
        return $discount->wpsc_cart_discount_number = '8';
    }   if ($total >= 1500 && $total < 3000) {
        return $discount->wpsc_cart_discount_number = '11';
    }   if ($total >= 3000 && $total < 7000) {
        return $discount->wpsc_cart_discount_number = '13';
    }  if ($total >= 7000 && $total < 10000) {
        return $discount->wpsc_cart_discount_number = '16';
    }  if ($total >= 10000) {
        return $discount->wpsc_cart_discount_number = '18';
    }
}

When I call this function it keep returning only value of "8" and not others. What is wrong here?

Upvotes: -1

Views: 90

Answers (2)

Halfwarr
Halfwarr

Reputation: 8103

Just taking a look at it I'm guessing you don't have $total being defined. Can you put some debug code and see if you $total has a value?

Or post more of the code and where $total is being defined.

Upvotes: 0

mcserep
mcserep

Reputation: 3299

Based on the fact, that you do not have a local $total variable in your function, it is undefined. By the flexible error handling of PHP, this would be only a notice, not an error. An undefined value is treated as 0 when compared to integers, therefore the first condition will be always be true.

Upvotes: 1

Related Questions