PHP - 简单的 IF ... ELSE IF... 产生意想不到的结果,请教我?

发布于 2024-12-07 15:02:48 字数 1056 浏览 1 评论 0原文

我在一段简单的 php 代码上遇到了麻烦。

我正在使用 2 个产品定价层。这些基于用户是否已登录。

如果用户未登录,且第一个价格为空;那么价格就是price1。如果不是,则为price1。

这工作得很好。

如果用户已登录,并且第一个价格为空;那么价格就是price1。如果不是,则为price2。

这是它应该工作的方式,但实际发生的情况是这样的:

如果用户登录,并且第一个价格为空;那么价格就是0。如果不是,那就是price2。

为什么我的代码会产生这种效果?

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

如果有人有任何建议可以帮助我解决这个问题,我将不胜感激。

谢谢你!

@Pekka,这相当复杂。我只是希望这种情况发生:

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

如果用户已登录但price2字段为空,则price变量将为price1。如果不是,则为price 2。

另一方面,如果用户未登录但price2 字段为空,则price 变量将为price1。如果没有,则价格为1。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霊感 2024-12-14 15:02:48

您解释规则的方式有点令人困惑。您应该能够修改以下代码以适应:

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

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;
}
落花随流水 2024-12-14 15:02:48

该代码与您描述的算法矛盾。

你告诉:

如果用户未登录且第一个价格为空,则
价格是价格1。如果没有,那就是价格2。如果用户已登录并且
第一个价格为空,则价格为price1。如果没有,则为price2。

因此,事实上,无论用户是否登录,算法都应该执行完全相同的操作。

在你的代码中,这也很奇怪:

你有三个条件:

  1. 用户未登录
  2. 用户已登录
  3. 其他

用户已登录或未登录。我没有看到任何其他可能性。

而且,以下行:

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

可以减少为,

$prPrice = $prPrice1;

因为您在两个代码块中执行相同的操作。

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.

云雾 2024-12-14 15:02:48

您的代码过于复杂并且包含一些奇怪的结构。这是一个等效但简化的版本,但请检查我在第一个 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;
    }
} 

用三元语句表达相同条件的稍微奇特的方式(示例相当于外部 if 的 else 块:

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

编辑

假设有一种方法可以区分尚未登录的帐户持有人和没有帐户的用户,您需要在外部条件下处理它,如下所示:

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;
}

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;
}
一梦浮鱼 2024-12-14 15:02:48
else 
{
    $prPrice = $prPrice1;
}

您无法达到此条件,因为据我了解 userIsLoggedIn 是布尔值,并且可能只发生两种情况:当 userIsLoggedIn 为 true 时和当 userIsLoggedIn 为 false 时。
你写的

如果用户未登录且第一个价格为空,则价格为price1。如果没有,则为price2。

但在你的代码中会发生这种情况:

如果用户未登录且第二价格为空,则价格为price1。如果不是,则也是price1

同样在 userIsLoggedIn 条件下您写道

如果用户登录并且第一个价格为空

但在您的代码中,您检查第二个价格是否为空。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文