我有以下if语句正常工作:
if ($user['email_only'] === '1') {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090438');
} else if($user['email_only'] === '0') {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090442');
}
但是,我想在此周围添加一个额外的if语句,以便我可以根据用户所在国家/地区设置不同的ID,我想出了以下内容,但它不起作用?
if ($user['country'] === 'au') {
if($user['email_only'] === '1') {
$newsubscriber['subscriberMessage'] = array('masterId' => '1091479');
} else {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090589');
}
} // AU country END
else if ($user['country'] === 'de') {
if($user['email_only'] === '1') {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090438');
} else {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090442');
}
} // DE country END
有人可以帮助我解决我的错误吗?
答案 0 :(得分:2)
不要使用完全相等的(===
),除非您需要并且绝对确定您获得的数据。试试这个:
if ($user['country'] == 'au') {
if($user['email_only'] == '1') {
$newsubscriber['subscriberMessage'] = array('masterId' => '1091479');
} else {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090589');
}
} // AU country END
else if ($user['country'] == 'de') {
if($user['email_only'] == '1') {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090438');
} else {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090442');
}
} // DE country END
您可以做的另一件事是var_dump($user);
,以确保您在country和email_only中具有您期望的预期值。
答案 1 :(得分:-1)
实际上,你应该正确地格式化代码......我也不会在这个混乱中找到错误。
if ($user['country'] === 'au'){
if ($user['email_only'] === 1) {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090438');
}else if($user['email_only'] === 0){
$newsubscriber['subscriberMessage'] = array('masterId' => '1090442');
}//AU END
} else if ($user['country'] === 'de'){
if ($user['email_only'] === 1) {
$newsubscriber['subscriberMessage'] = array('masterId' => '1090438');
}else if($user['email_only'] === 0){
$newsubscriber['subscriberMessage'] = array('masterId' => '1090442');
}
} //DE END