我使用PHP中的if语句。我要的是 - 如果用户不输入帐号或usernamd +密码,然后显示错误信息 - 但不工作,这是我的代码 -
if($add_data['social_id']!="" || $add_data['username'] && $add_data['password']=="")
{
//login with social id code here
}
elseif($add_data['username'] && $add_data['password']!="" || $add_data['social_id']=="")
{
//login with usernamd and password code here
}
else
{
//please enter social id or username with password
}
如果变量尚未设置,即!=""
使用true
可能返回null != ""
。您可以通过使用empty()
克服这个
if(!empty($add_data['social_id']))
{
//login with social id code here
}
elseif(!empty($add_data['username']) && !empty($add_data['password']))
{
//login with username and password code here
}
else
{
//please enter social id or username with password
}
$add_data['username'] && $add_data['password'] == ""
是不是做你期待什么。你不能factorise条件这样。
相反,使用$add_data['username'] == "" && $add_data['password'] == ""
欲了解更多可读性,做同样由$add_data['username'] && $add_data['password'] != ""
更换$add_data['username'] != "" && $add_data['password'] != ""
(但是,这个条件可以发挥预期的,见下文)
这样做if ($var && $var2 == "")
将检查$var
是不假,如果$var2
等于""
不是假的(又名真)比不同的东西:
所有其它值都被认为是true
你需要在if语句每个属性后面添加一个表达式/值:
if($add_data['social_id']!="" || $add_data['username'] != "" && $add_data['password']=="")
{
//login with social id code here
}
// add beside $add_data any expression
elseif($add_data['username'] != "" && $add_data['password']!="" || $add_data['social_id']=="")
{
//login with usernamd and password code here
}
else
{
//please enter social id or username with password
}
其次,我不明白你想摆在那里是什么情况?如果社会的id是不是null,用户名或密码不为空?或全部应输入。
请分享这个,所以我可以给你确切的代码。