Php 意外的其他

Php 意外的其他,php,function,if-statement,Php,Function,If Statement,我已经有了一段我创建的代码,并且我已经在我的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 th

我已经有了一段我创建的代码,并且我已经在我的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";
    }
}
}
函数howLongAgo($unix\u时间){
//此函数显示unix时间戳
//以秒、分钟、小时、天、月、年为单位
//获取指定时间与当前unix时间之间的时间差
$time\u difference=time()-$unix\u time;
$seconds\u ago=$time\u差;
$minutes\u ago=$time\u difference/60;
$hours\u ago=$time\u difference/3600;
$days\u ago=$time\u difference/86400;
$months\u ago=$time\u difference/2629743;
$年前=$时差/31556926;
如果($时差<60){
//几秒钟前
返回回合($时差)。“秒前”;
}如果($时差>=60)和($时差<3600)){
//几分钟前
if(四舍五入($time_difference/60)==1){
返回回合(时差/60)。“分钟前”;
}否则{
返回回合(时差/60)。“分钟前”;
}
}如果($timeu-difference>=3600&$timeu-difference<86400){
//几小时前
if(四舍五入($time_difference/3600)==1){
返回回合(时差/3600)。“小时前”;
}否则{
返回回合(时差/3600)。“小时前”;
}
}如果($时差>=86400&$时差<2629743){
//几天前
if(四舍五入($time_difference/86400)==1){
返回轮($时差/86400)。“前一天”;
}否则{
返回轮($时差/86400)。“几天前”;
}
}否则,如果($timeu-difference>=2629743&&$timeu-difference<31556926){
//几个月前
if(四舍五入($时差/2629743)==1){
返回轮($时差/2629743)。“一个月前”;
}否则{
返回轮(时差/2629743)。“几个月前”;
}
}否则如果($时差>=31556926){
//多年前
if(四舍五入($时差/31556926)==1){
返回轮($时差/31556926)。“一年前”;
}否则{
返回轮($时差/31556926)。“年前”;
}
}
}

尽管我在代码中根本没有调用该函数,但还是出现了这个错误


有人能看到代码中的任何错误吗,因为我看不到:(

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

从PHP:

注意:请注意,仅当使用上述示例中的花括号时,elseif和else if才会被视为完全相同。当使用冒号定义if/elseif条件时,不能将else if分隔为两个单词,否则PHP将因解析错误而失败


您的代码在第47行出现语法错误(我敢打赌您在第46行缺少半彩色或括号)

不管您是否调用该函数,php解析器都会预先分析整个文件。

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

使用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;

}
?>


请指出第47行您提供的源代码是正确的。问题似乎在其他地方。“即使我根本没有调用该函数”。这没关系,解析就是解析,即使它没有被调用(这首先不是一件好事,为什么有它呢?)您发布的代码在语法上是正确的:但他在所有地方都使用了花括号,不是吗?从文档中可以看出:
elseif
else if
在使用上述示例中的花括号时,只会被认为是完全相同的……因此,在这里不应该有什么区别。不,它们的目的是相同的这个例子的操作系统。
}else if{}
}else{if{}
完全相同。这只是惯例……还有卢克,这里是手册中切换功能的链接:嗨,凯文,非常感谢你花时间为我修改代码,现在代码更整洁了,切换案例也更符合逻辑。