0
{{
    if( sfprice_administrator_applicable() && sfprice_get_administrator_price($product) > 0 )
        $price = sfprice_get_administrator_price($product);
    return $price; 
}
{   elseif( sfprice_resellerplus_applicable() && sfprice_get_resellerplus_price($product) > 0 )
        $price = sfprice_get_resellerplus_price($product);
    return $price;
}
{    elseif( sfprice_reseller_applicable() && sfprice_get_reseller_price($product) > 0 )
        $price = sfprice_get_reseller_price($product);
    return $price;
}
{   elseif( sfprice_corporateplus_applicable() && sfprice_get_corporateplus_price($product) > 0 )
        $price = sfprice_get_corporateplus_price($product);
    return $price;
}
{   elseif( sfprice_corporate_applicable() && sfprice_get_corporate_price($product) > 0 )
        $price = sfprice_get_corporate_price($product);
    return $price;
}
{   else( sfprice_smallbusiness_applicable() && sfprice_get_smallbusiness_price($product) > 0 )
        $price = sfprice_get_smallbusiness_price($product);
    return $price;
}}

I am sure that this is very wrong but i am extremely new to programming so i would appreciate direction on how this should have been coded?

I understand my issue is with the if/elseif/else statement structure. But i am unsure how to properly format the information.

5
  • 1
    From the manual Commented Aug 12, 2015 at 20:45
  • No this breaks the entire site; if i have only 1 if statement it works perfectly with everything else. Commented Aug 12, 2015 at 20:46
  • I'm of the opinion that if you need to use an elseif you should probably be using a switch instead. Commented Aug 12, 2015 at 20:53
  • @Blake You can't always use switch statements if you have many conditions. A switch will only let you choose one case, where as if you have many conditions, a switch won't do the job. Commented Aug 12, 2015 at 20:54
  • Was speaking generally, in this case specifically since they are going to return on every if eval that is true, might as well just have only if statements. It'd be way easier to read, too. Write code so you (and anyone else) save the most cost from the most valuable resource.. humans. Commented Aug 12, 2015 at 20:57

1 Answer 1

1

Your problem is how you bracket your if-statements. How they are bracketed and formatted below is how it should look.

Use this code instead

if (sfprice_administrator_applicable() && sfprice_get_administrator_price($product) > 0 ) {
    $price = sfprice_get_administrator_price($product);
    return $price; 
} elseif (sfprice_resellerplus_applicable() && sfprice_get_resellerplus_price($product) > 0 ) {
    $price = sfprice_get_resellerplus_price($product);
    return $price;
} elseif (sfprice_reseller_applicable() && sfprice_get_reseller_price($product) > 0 ) {
    $price = sfprice_get_reseller_price($product);
    return $price;
} elseif (sfprice_corporateplus_applicable() && sfprice_get_corporateplus_price($product) > 0 ) {
    $price = sfprice_get_corporateplus_price($product);
    return $price;
} elseif (sfprice_corporate_applicable() && sfprice_get_corporate_price($product) > 0 ) {
    $price = sfprice_get_corporate_price($product);
    return $price;
} else (sfprice_smallbusiness_applicable() && sfprice_get_smallbusiness_price($product) > 0 ) {
    $price = sfprice_get_smallbusiness_price($product);
    return $price;
}

Note that PHP runs in a way that selects the first elseif that returns true.

Sign up to request clarification or add additional context in comments.

9 Comments

Ah so the brackets needed to be set after the elseif statements and then return $price outside of the brackets after each statement so that if it returned true then it would return that price?
The brackets are to be placed as the code given in my answer, yes. But the return $price; is inside the brackets! If you look, the closing-bracket is on the next line, just before the elseif-statement.
I've been looking at this too long apparently everything is running together! But yes i do see it now. Thank you very much I appreciate the help there.
Happy to help! We all started somewhere ;)
Kind of hard to play "spot the mistake" without any errors. What does your error_log say?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.