【发布时间】:2017-06-03 16:58:45
【问题描述】:
我正在尝试改进下面的 switch 语句。这里发生的情况是,根据找到的 x 个令牌多次调用代码,因此下面的代码每个令牌运行一次。
如果未找到$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;
}
【问题讨论】:
标签: php wordpress switch-statement