我正在尝试改进下面的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