我正在嘗試改進下面的switch語句。這裏發生的事情是,基於x個找到的令牌多次調用代碼,所以下面的代碼每個令牌運行一次。在PHP中改進switch語句或建議替代解決方法
如果找不到$post->ID
,則會向該令牌發送通知,並將該ID添加到數據庫中。
但是在某些情況下,它可以在檢測到大約40%的令牌後停止,這大概是因爲找到了ID?由於我在wordpress上,我使用update_option
將ID存儲在表中,但可能使用其他方法?
$os = $this->os;
switch ($os) {
case "iOS":
$iOS_pastPushSavedID = get_option('iOS_pastPushSavedID', $default = false);
if($post->ID != $iOS_pastPushSavedID) {
update_option('iOS_pastPushSavedID', $post->ID, no);
$sendPush = true;
//$title = ($os . '_New Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID);
} else {
//$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID);
$sendPush = false;
}
break;
case "Android":
$android_pastPushSavedID = get_option('android_pastPushSavedID', $default = false);
if($post->ID != $android_pastPushSavedID) {
//$title = ($os . '_New Push = ' . ' storedID: ' . $android_pastPushSavedID . '/postID: ' . $post->ID);
update_option('android_pastPushSavedID', $post->ID, no);
$sendPush = true;
} else {
//$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $android_pastPushSavedID . '/postID: ' . $post->ID);
$sendPush = false;
}
break;
case "Fire OS":
$fireos_pastPushSavedID = get_option('fireos_pastPushSavedID', $default = false);
if($post->ID != $fireos_pastPushSavedID) {
//$title = ($os . '_New Push = ' . ' storedID: ' . $fireos_pastPushSavedID . '/postID: ' . $post->ID);
update_option('fireos_pastPushSavedID', $post->ID, no);
$sendPush = true;
} else {
//$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $fireos_pastPushSavedID . '/postID: ' . $post->ID);
$sendPush = false;
}
break;
case "Safari":
$safari_pastPushSavedID = get_option('safari_pastPushSavedID', $default = false);
if($post->ID != $safari_pastPushSavedID) {
//$title = ($os . '_New Push = ' . ' storedID: ' . $safari_pastPushSavedID . '/postID: ' . $post->ID);
update_option('safari_pastPushSavedID', $post->ID, no);
$sendPush = true;
} else {
//$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $safari_pastPushSavedID . '/postID: ' . $post->ID);
$sendPush = false;
}
break;
case "Chrome":
$chrome_pastPushSavedID = get_option('chrome_pastPushSavedID', $default = false);
if($post->ID != $chrome_pastPushSavedID) {
//$title = ($os . '_New Push = ' . ' storedID: ' . $chrome_pastPushSavedID . '/postID: ' . $post->ID);
update_option('chrome_pastPushSavedID', $post->ID, no);
$sendPush = true;
} else {
//$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $chrome_pastPushSavedID . '/postID: ' . $post->ID);
$sendPush = false;
}
break;
case "Firefox":
$firefox_pastPushSavedID = get_option('firefox_pastPushSavedID', $default = false);
if($post->ID != $firefox_pastPushSavedID) {
//$title = ($os . '_New Push = ' . ' storedID: ' . $firefox_pastPushSavedID . '/postID: ' . $post->ID);
update_option('firefox_pastPushSavedID', $post->ID, no);
$sendPush = true;
} else {
//$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $firefox_pastPushSavedID . '/postID: ' . $post->ID);
$sendPush = false;
}
break;
default:
$sendPush = false;
}
謝謝!將檢查出來並恢復。感謝您的快速回復。你會建議一個更快的替代方法來將var存儲在MySQL Wordpress中嗎?或者我的解決方案好嗎? Tx – Jason
@Jason歡迎...我認爲它是好的,這是你在這裏試圖做的事情的最短代碼。我的代碼,我給你一種方式,你可以防止自己重寫相同的代碼.. :) –
謝謝!明天會試試這個,並會讓你知道! – Jason