PHP返回 - 初学者错误

时间:2014-08-26 11:13:14

标签: php return

我不知道我做错了什么。也许有人可以建议我。我正在尝试定义季节,然后在索引中使用结果。

...

带功能的文件:

function getCurrentTheme($for_area) {

// Definition of seasons
$spring='spring theme title';
$spring_season=array('03-21',......);
$summer='summer theme title';
$summer_season=array(......);
$autumn='autumn theme title';
$autumn_season=array(......);
$winter='winter theme title';
$winter_season=array(......);

// Today
$current_date=date('m-d');

// So what season is now?
if ($for_area==='some_area') {
    if (in_array($current_date,$spring_season) {
        $theme=$spring;
    }
    else if (in_array($current_date,$summer_season) {
        $theme=$summer;
    }
    else if (in_array($current_date,$autumn_season) {
        $theme=$autumn;
    }
    else if (in_array($current_date,$winter_season) {
        $theme=$winter;
    }
    else {}
}
else if ($for_area==='other_area') {
    // ...
}
else {}

return $theme;

}

...

索引:

$area='some_area';
getCurrentTheme($area);

// And here is the fault. Hope sometimes I will stop being retarded.

echo $theme;

// What should be printed?
summer theme title

提前致谢,请尝试了解我的清白。

2 个答案:

答案 0 :(得分:1)

您需要存储该值,以便您可以回显它:

$area='some_area';
$theme = getCurrentTheme($area);    
echo $theme;

答案 1 :(得分:1)

您没有存储从getCurrentTheme函数返回的值。将其存储在变量中,然后echo将其存储在

$theme = getCurrentTheme($area);
echo $theme;

或简单地回应呼叫而不必使用变量。

echo getCurrentTheme($area);

您的选择!

相关问题