【问题标题】:Unexpected T_ELSE [closed]意外的 T_ELSE [关闭]
【发布时间】:2011-09-07 14:51:36
【问题描述】:

我已经创建了一段代码,并且我已经在我的 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";
    }
}
}

这个错误即将出现,即使我的代码中根本没有调用该函数。

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

【问题讨论】:

  • 请指出第47行
  • 您提供的来源是正确的。问题似乎出在其他地方。
  • “即使我根本没有调用该函数”。没关系,解析就是解析,即使它没有被调用(这首先不是一件好事,为什么还要它呢?)
  • 您发布的代码在语法上是正确的:codepad.org/YbYI0Rp4

标签: php function if-statement


【解决方案1】:

使用 switch/case 逻辑方案会简单得多。我已经使用 switch/case 重新编码了你在那里的内容,这可能是你学习 switch/case 的一个很好的介绍。在上面的 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;

}
?>

【讨论】:

【解决方案2】:

我认为您的问题可能来自 elseif 和 else if 之间的差异(注意空格)。

来自 PHP website:

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

【讨论】:

  • 但是他到处都用大括号,不是吗?
  • 来自文档:请注意,elseifelse if 只有在使用大括号时才会被视为完全相同。 ... 所以它在这里不应该有所作为。
  • 不,就本示例而言,它们是相同的构造。 } else if {} 的处理方式与 } else { if { } } 完全相同。这只是惯例......
【解决方案3】:

您的代码在第 47 行有语法错误(我敢打赌,您在第 46 行缺少半色或括号。

是否调用函数无关紧要,因为 php 解析器会预先分析整个文件。

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2012-05-19
      相关资源
      最近更新 更多