這裏是我的代碼:通過PHP初始化超全局代碼是否正確?
$order_newest = $order_votes = $order_featured = $order_frequent = '';
if (isset($_GET['o']) || isset($_COOKIE['qanda_questions_order'])) {
// To read from the cookie
if (!isset($_GET['o'])) {
$_GET['o'] = $_COOKIE['qanda_questions_order'];
} else {
setcookie("qanda_questions_order", $_GET['o'], 2147483647);
}
switch ($_GET['o']) {
case 'newest':
$order_newest = 'order_active';
break;
case 'votes':
$order_votes = 'order_active';
break;
case 'featured':
$order_featured = 'order_active';
break;
case 'frequent':
$order_frequent = 'order_active';
break;
default:
$order_newest = 'order_active';
break;
}
} else {
$order_newest = 'order_active';
}
正如你看到的,我已經初始化supergobal在這條線:
$_GET['o'] = $_COOKIE['qanda_questions_order'];
做一個正確的事情?或者只應該使用超級對象作爲URL (獲取方法)中的傳遞參數?
也可以寫這個邏輯更好嗎? (對我來說似乎不專業)
爲什麼要使用超全局而不是另一個變量? –
@ Don'tPanic因爲我在*'switch'中使用了超全局* –
這非常好。它使你不必執行'$ o = $ _GET ['o'];'然後使用'$ o'。 – AbraCadaver