开发者

PHP - Simple IF ... ELSE IF... producing unexpected results, educate me?

I am having trouble with a simple peice of php code.

I am working with 2 product pricing tiers. These are based on whether a user has logged in, or not.

If a user is not logged in, and the first price is empty; then the price is price1. If not, it is price1.

This works perfectly fine.

If a user is logged in, and the first price is empty; then the price is price1. If not, it is price2.

This is the way it should work, but what actually happens is this:

If a user is logged in, and the first price is empty; then the price is 0. If not, it is price2.

Why is my code producing this effect?

if (!userIsLoggedIn())
{
    if (empty($prPrice2))
    {
        $prPrice = $prPrice1;
    }
    else
    {
        $prPrice = $prPrice1;
    }
} else if (userIsLoggedIn())
{
    if (empty($prPrice2))
    {
        $prPrice = $prPrice1;
    }
    else
    {
        $prPrice = $prPrice2;
    }
} else 
{
    $prPrice = $prPrice1;
}

If anyone has any suggestions that could help me to resolve this issue, it would be greatly appreciated.

Thank you!

@Pekka, it is fairly complicated. I simply would like this to happen:

product 1 -> price 1 = 1.00
product 1 -> price 2 = 0.00
product 2 -> price 1 = 1.00
product 2 -> price 2 = 0.80

If a user is logged in but the price2 field is empty, then the price variable will be price1. if not, then it will be price 2.

On the other hand, if a user is not logged in but the price2 field is empty, then the price variable will be price1. if not, then it will开发者_如何转开发 be price 1.


The way you explained the rules was a bit confusing. You should be able to modify the below code to fit:

if (userIsLoggedIn()) {
    $prPrice = !empty($prPrice2) ? $prPrice2 : $prPrice1;
} else {
    $prPrice = !empty($prPrice1) ? $prPrice1 : $prPrice2;
}


The code is in contradiction with the algorithm you described.

You told:

If a user is not logged in and the first price is empty, then the price is price1. If not, it is price2. If a user is logged in and the first price is empty, then the price is price1. If not, it is price2.

So, in fact, the algorithm should do exactly the same thing whether the user is logged in or not.

In your code, it's also very strange:

You have three conditions:

  1. user is not logged in
  2. user is logged in
  3. other

The user is logged in or it's not logged in. I don't see any other possibility.

And also, the following lines:

if (empty($prPrice2))
{
    $prPrice = $prPrice1;
}
else
{
    $prPrice = $prPrice1;
}

could be reduced to

$prPrice = $prPrice1;

since you're doing the same operation in the two code blocks.


You code is overly complicated and contains some odd constructions. This is an equivalent but simplified version, but check the comment I added on the first else....

if (!userIsLoggedIn())
{
    if (empty($prPrice2))
    {
        $prPrice = $prPrice1;
    }
    else
    {
        $prPrice = $prPrice1;  // this is highly suspicious...
    }
} else {    // user is logged or not, no need to recheck that boolean var
    if (empty($prPrice2))
    {
        $prPrice = $prPrice1;
    }
    else
    {
        $prPrice = $prPrice2;
    }
} 

a slightly fancier way of expressing the same condition with the ternary statement (example is equivalent to else block of the outer if:

$prPrice = (empty($prPrice2)) ? $prPrice1 : $prPrice2;

EDIT

Assuming that there is a way to differentiate between accountholders that haven't logged in and users without accounts, your need to handle that in an outer condition, like this:

if ($UserHasAnAccount) {  
// but i don't understand (yet) how you'd know that at this point
    if (userIsLoggedIn()) {  // the price logic described before
        ....
    } else {
        ....
    }
} else {  // unknown user, price1
    $prPrice = $prPrice1;
}


else 
{
    $prPrice = $prPrice1;
}

You can't reach this condition because as I understand userIsLoggedIn is boolean and it may happen only two conditions: when userIsLoggedIn is true and when it false. You wrote that

If a user is not logged in and the first price is empty, then the price is price1. If not, it is price2.

But in you code this happens:

If a user is not logged in and the second price is empty, then the price is price1. If not, it is price1 too.

Also in the condition userIsLoggedIn you wrote that

If a user is logged in and the first price is empty

But in your code you check if the second price is empty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜