意外的T_ELSE [英] Unexpected T_ELSE

查看:127
本文介绍了意外的T_ELSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 解析错误:语法错误,在第47行的/home/makequiz/public_html/nitpicker/func.php中出现意外的T_ELSE 

这是函数:

 函数howLongAgo($ unix_time){
//此函数显示unix时间戳
//作为秒数,分钟数,小时数,天数,月数,年前数


//获取指定时间与当前unix时间之间的时间差
$ 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;
$ b $如果($ time_difference <60){
//秒前
返回一轮($ time_difference)。 '秒以前'; $(b
$ b)else if(($ time_difference> = 60)&&($ time_difference <3600)){

//分钟前
if ($($ time_difference / 60)== 1){
return round($ time_difference / 60)。 一分钟前;
} else {
return round($ time_difference / 60)。 几分钟前;
}

} else if($ time_difference> = 3600&& $ time_difference< 86400){
//小时前
if(round $ time_difference / 3600)== 1){
return round($ time_difference / 3600)。 一个小时前;
} else {
return round($ time_difference / 3600)。 小时前;
}
} else if($ time_difference> = 86400&& $ time_difference< 2629743){
//天数
if(round($ time_difference / 86400 )== 1){
return round($ time_difference / 86400)。 天之前;
} else {
return round($ time_difference / 86400)。 几天前;
}
} else if($ time_difference> = 2629743&& $ time_difference< 31556926){
//月前
if(round($ time_difference / 2629743 )== 1){
return round($ time_difference / 2629743)。 一个月前;
} else {
return round($ time_difference / 2629743)。 几个月前;
}
} else if($ time_difference> = 31556926){
// Years Ago
if(round($ time_difference / 31556926)== 1){
返回($ time_difference / 31556926)。 一年前;
} else {
return round($ time_difference / 31556926)。 多年前;



code
$ b

这个错误即将到来,即使我没有在我的代码中调用函数。



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

解决方案

使用switch / case逻辑方案会更简单一些,我使用switch / case重新编写了那些代码,这可能是你学习switch / case的一个很好的介绍,你在上面的if语句中有一些额外的(),我希望这会有所帮助,朋友:

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

函数howLongAgo($ time_difference){

//基于传递给这个函数的时间差的Swtich逻辑,设置英文字符串和差值需要除以
switch($ time_difference){
case($ time_difference <60):
$ string =second;
break;
情况($ time_difference> = 60&& $ time_difference< 3600):
$ string =minute;
$ divider = 60;
休息;
case($ time_difference> = 3600&& $ time_difference< 86400):
$ string =hour;
$ divider = 3600;
休息;
case($ time_difference> = 86400&& $ time_difference< 2629743):
$ string =day;
$ divider = 86400;
休息;
case($ time_difference> = 2629743&& $ time_difference< 31556926):
$ string =month;
$ divider = 2629743;
休息;
case($ time_difference> = 31556926):
$ string =year;
$ divider = 31556926;
休息;


//如果在交换过程中设置了分隔符值,则使用它获取实际差异
如果($ divider){$ diff = round($ time_difference /如果差异不等于1,则复制最终结果EG:小时,分钟,秒
if($ diff);} else {$ diff = round($ time_difference);}
//如果!= 1){$ pluralize =s;}
//连接所有变量并返回它们
$ final = $ diff。 $字符串。 $ pluralize。 前;
返回$ final;

}
?>


I've got a piece of code that I created and I've been 'requiring_once' in to my PHP code for quite a while now. It works fine on my local PC, but when I upload it to my server it's coming up with the following error:

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

Here's the function:

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";
    }
}
}

This error is coming up, even though I haven't called the function at all in my code.

Can anyone see any errors in the code, cause I can't :(

解决方案

This would be a lot simpler using a switch/case logic scheme. I have re-coded what you had up there using switch/case and this might be a good intro for you to learn switch/case. You had some extra ( ) in one of the if statements above. I hope this helps, friend:

<?
$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;

}
?>

这篇关于意外的T_ELSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆