PHP - 简单的 IF ... ELSE IF... 产生意想不到的结果,请教我?
我在一段简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您解释规则的方式有点令人困惑。您应该能够修改以下代码以适应:
The way you explained the rules was a bit confusing. You should be able to modify the below code to fit:
该代码与您描述的算法矛盾。
你告诉:
因此,事实上,无论用户是否登录,算法都应该执行完全相同的操作。
在你的代码中,这也很奇怪:
你有三个条件:
用户已登录或未登录。我没有看到任何其他可能性。
而且,以下行:
可以减少为,
因为您在两个代码块中执行相同的操作。
The code is in contradiction with the algorithm you described.
You told:
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:
The user is logged in or it's not logged in. I don't see any other possibility.
And also, the following lines:
could be reduced to
since you're doing the same operation in the two code blocks.
您的代码过于复杂并且包含一些奇怪的结构。这是一个等效但简化的版本,但请检查我在第一个 else... 上添加的注释...
用三元语句表达相同条件的稍微奇特的方式(示例相当于外部 if 的 else 块:
编辑
假设有一种方法可以区分尚未登录的帐户持有人和没有帐户的用户,您需要在外部条件下处理它,如下所示:
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....
a slightly fancier way of expressing the same condition with the ternary statement (example is equivalent to else block of the outer if:
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:
您无法达到此条件,因为据我了解
userIsLoggedIn
是布尔值,并且可能只发生两种情况:当 userIsLoggedIn 为 true 时和当 userIsLoggedIn 为 false 时。你写的
但在你的代码中会发生这种情况:
同样在 userIsLoggedIn 条件下您写道
但在您的代码中,您检查第二个价格是否为空。
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
But in you code this happens:
Also in the condition userIsLoggedIn you wrote that
But in your code you check if the second price is empty.