2016-03-01 85 views
-3

我昨天發佈了代碼中的很多錯誤,所以我去了並重新開始。代碼沒有顯示任何東西是否有一些即時消息missing.Im新到這麼試當我加載到我的本地主機時,它返回的是'無效季節'PHP代碼。顯示季節(第2輪)

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 

$season = array('winter','spring','summer','autumn'); 

function getWinterMonths(){ 
$WinterMonths = array(
1 => array('November'), 
2 => array('December'), 
3 => array('January') 
); 
return $WinterMonths; 
} 

function getSpringMonths(){ 
$SpringMonths = array(
1 => array('February'), 
2 => array('March'), 
3 => array('April') 
); 
return $SpringMonths; 
} 


function getSummerMonths(){ 
$SummerMonths = array(
1 => array('May'), 
2 => array('June'), 
3 => array('July') 
); 
return $SummerMonths; 
} 

function getAutumnMonths(){ 
$AutumnMonths = array(
1 => array('August'), 
2 => array('September'), 
3 => array('October') 
); 
return $AutumnMonths; 
} 



if($season == "winter") 
echo $WinterMonths(); 

else if($season == "spring") 
echo $SpringMonths(); 

else if($season == "summer") 
echo $SummerMonths(); 

else if($season == "autumn") 
echo $AutumnMonths(); 
else{ 
echo "Invalid season entred"; 
} 
+2

你不叫「getSeasonMonths」,但你所說的變量,它被聲明爲函數($ seasonMonths)...範圍的問題,對不對?順便說一句,$季節是一個數組,它不能是==「夏天」!但$季節[1]或[0]可以... – Julo0sS

+0

你能說出你正在試圖存檔嗎? – Kordi

回答

0

您的代碼似乎無效。例如,你用一個數組比較的字符串:

$season = array('winter','spring','summer','autumn'); 
/* more stuff */ 
else if($season == "summer") 

雖然$season它是一個數組,這種情況將永遠false。如果更改比較喜歡這樣的工作原理:

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 

$arrSeason = array('winter','spring','summer','autumn'); 

function getWinterMonths(){ 
$WinterMonths = array(
1 => array('November'), 
2 => array('December'), 
3 => array('January') 
); 
return $WinterMonths; 
} 

function getSpringMonths(){ 
$SpringMonths = array(
1 => array('February'), 
2 => array('March'), 
3 => array('April') 
); 
return $SpringMonths; 
} 


function getSummerMonths(){ 
$SummerMonths = array(
1 => array('May'), 
2 => array('June'), 
3 => array('July') 
); 
return $SummerMonths; 
} 

function getAutumnMonths(){ 
$AutumnMonths = array(
1 => array('August'), 
2 => array('September'), 
3 => array('October') 
); 
return $AutumnMonths; 
} 


$season = $_POST['season']; /* For example get from post */ 
if($season == "winter") 
    echo getWinterMonths(); 

else if($season == "spring") 
    echo getSpringMonths(); 

else if($season == "summer") 
    echo getSummerMonths(); 

else if($season == "autumn") 
    echo getAutumnMonths(); 
else{ 
    echo "Invalid season entred"; 
} 
+0

即時通訊解析錯誤:語法錯誤,意外的'var'(T_VAR)在C:\ wamp \ www \ 3 \ script.php 44行 –

+0

對不起,我用javascript LOL編寫。我編輯沒有VAR,但你不能複製和粘貼,你需要了解代碼 –

+0

是的,這是真的。 –

0
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 

只檢查,如果你有POST方法,就不會得到這個職位的變量,你需要使用$ _ POST了點。

你沒有向函數傳遞任何東西,我假設你想通過$ _POST傳遞季節,所以你需要沿着以下幾行: $_POST['season'];

希望幫助

+0

我有一個索引頁面,您可以在這裏發佈季節,並且您輸入的季節顯示該季節的月份。關於如此模糊 –