2009-06-21 125 views
0
$method = 'post'; 

$method = strtoupper($method); 
echo $method.'test1'; 

$method = '_'.$method; 
echo $method.'test2'; 

$method = $$method; 
echo $method.'test3'; 

爲什麼不打印2到3之間的$ _POST內容?驗證變量變量不起作用

回答

1

您想要$method['test3']訪問$_POST陣列的元素。點.運算符執行字符串連接。方括號[]用於數組訪問。

+0

不,我想$ _SESSION,$ _COOKIE,$ _ POST和$ _GET從 '會議', '餅乾', '後' 和「G等」。 – 2009-06-21 02:47:16

+0

我想我最終會使用 if($ method =='post')$ method = $ _POST; if($ method =='get')$ method = $ _GET; if($ method =='session')$ method = $ _SESSION; if($ method =='cookie')$ method = $ _COOKIE; – 2009-06-21 02:49:35

1

除了約翰Kugelman的非常好的一點,我會用以下

$method = $_POST; 

echo $method['test1']; 

echo $method['test2']; 

echo $method['test3']; 

,而不是與試圖通過串

訪問含量的不同數組名稱上使用。如果你真的堅持打擾一個字符串訪問這些,你可以

$method = "post"; 
$method = strtoupper($method."_");  
if (isset(${$method})) { 
    $method = ${$method}; 

    echo $method['test1']; 

    echo $method['test2']; 

    echo $method['test3']; 
}