PHP代码。显示季节(第2轮)

时间:2016-03-01 11:38:25

标签: php

我昨天发布了,但代码中出现了很多错误,所以我又重新开始了。代码没有显示任何东西,有什么东西我不知道。我是新手,所以试着去学习。当我把它加载到我的本地所有它返回的东西是'无效季节'

<?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 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分:0)

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

只检查你是否有POST方法,它不会获得post变量,你需要使用$ _POST。

你没有传递任何功能,我认为你想通过$ _POST传递赛季,所以你需要的东西是: $_POST['season'];

希望有所帮助