意外的T_ELSE

时间:2011-09-07 14:51:36

标签: php function if-statement

我已经创建了一段代码,而且我已经'require_once'进入我的PHP代码已经有一段时间了。它在我的本地PC上工作正常,但是当我将它上传到我的服务器时,它会出现以下错误:

Parse error: syntax error, unexpected T_ELSE in /home/makequiz/public_html/nitpicker/func.php on line 47

这是功能:

function howLongAgo($unix_time) {
// This function shows the unix time stamp 
// as number of seconds, minutes, hours, days, months, years ago 


// Get the time difference between time specified and the current unix time 
$time_difference = time () - $unix_time;

$seconds_ago = $time_difference;
$minutes_ago = $time_difference / 60;
$hours_ago = $time_difference / 3600;
$days_ago = $time_difference / 86400;
$months_ago = $time_difference / 2629743;
$years_ago = $time_difference / 31556926;

if ($time_difference < 60) {
    // Seconds Ago 
    return round ( $time_difference ) . ' Seconds Ago';

} else if ( ($time_difference >= 60) && ($time_difference < 3600) ) {

    // Minutes Ago
    if (round ( $time_difference / 60 ) == 1) {
        return round ( $time_difference / 60 ) . " Minute Ago";
    } else {
        return round ( $time_difference / 60 ) . " Minutes Ago";
    }

} else if ($time_difference >= 3600 && $time_difference < 86400) {
    // Hours Ago 
    if (round ( $time_difference / 3600 ) == 1) {
        return round ( $time_difference / 3600 ) . " Hour Ago";
    } else {
        return round ( $time_difference / 3600 ) . " Hours Ago";
    }
} else if ($time_difference >= 86400 && $time_difference < 2629743) {
    // Days Ago 
    if (round ( $time_difference / 86400 ) == 1) {
        return round ( $time_difference / 86400 ) . " Day Ago";
    } else {
        return round ( $time_difference / 86400 ) . " Days Ago";
    }
} else if ($time_difference >= 2629743 && $time_difference < 31556926) {
    // Months Ago 
    if (round ( $time_difference / 2629743 ) == 1) {
        return round ( $time_difference / 2629743 ) . " Month Ago";
    } else {
        return round ( $time_difference / 2629743 ) . " Months Ago";
    }
} else if ($time_difference >= 31556926) {
    // Years Ago 
    if (round ( $time_difference / 31556926 ) == 1) {
        return round ( $time_difference / 31556926 ) . " Year Ago";
    } else {
        return round ( $time_difference / 31556926 ) . " Years Ago";
    }
}
}

即使我没有在我的代码中调用该函数,也会出现此错误。

任何人都可以在代码中看到任何错误,因为我不能:(

4 个答案:

答案 0 :(得分:4)

使用开关/案例逻辑方案会更加简单。我使用开关/外壳重新编码了你的东西,这可能是你学习开关/外壳的好介绍。你在上面的一个if语句中有一些额外的()。我希望这有帮助,朋友:

<?
$unix_time = 6734;
echo howLongAgo($unix_time);

function howLongAgo($time_difference){

// Swtich logic based on the time difference passed to this function, sets the english string and what number the difference needs to be divided by
    switch($time_difference){
         case ($time_difference < 60):
              $string = " second";
              break;
         case ($time_difference >= 60 && $time_difference < 3600):
              $string = " minute";
              $divider = 60;
              break;
         case ($time_difference >= 3600 && $time_difference < 86400):
              $string = " hour";
              $divider = 3600;
              break;
         case ($time_difference >= 86400 && $time_difference < 2629743):
              $string = " day";
              $divider = 86400;
              break;
         case ($time_difference >= 2629743 && $time_difference < 31556926):
              $string = " month";
              $divider = 2629743;
              break;
         case ($time_difference >= 31556926):
              $string = " year";
              $divider = 31556926;
              break;
    }

// If a divider value is set during the switch, use it to get the actual difference
if($divider){$diff = round($time_difference / $divider);}else{$diff = round($time_difference);}
// If the difference does not equal 1, pluralize the final result EG: hours, minutes, seconds
if($diff != 1){$pluralize="s";}
// Concatenate all variables together and return them
$final =  $diff . $string . $pluralize . " ago";
return $final;

}
?>

答案 1 :(得分:0)

我认为你的问题可能来自elseif和else之间的区别if(注意空格)。

来自PHP website

  

注意:请注意elseif和else在使用大括号时只会被视为完全相同,如上例所示。使用冒号来定义if / elseif条件时,如果是两个单词,则不能将else分开,否则PHP将因解析错误而失败。

答案 2 :(得分:0)

第47行的代码中存在语法错误(我打算在第46行丢失半色或括号。

如果您正在调用该函数,则无关紧要,因为php解析器将提前分析整个文件。

答案 3 :(得分:0)

一些事情...... 您是否更改了加载到服务器时包含的文件? 尝试使用include而不是require?