douxing5199 2015-08-07 07:09
浏览 34
已采纳

改为使用数组重写PHP函数

Is there any way I could rewrite this function with an array instead of all these if statements? Could i maybe use some for loop together with an array? How would that look like? Any suggestions of simpler code?

Here is my php function:

function to_next_level($point) {

    /*
    **********************************************************************
    *
    *   This function check how much points user has achievents and how much procent it is until next level
    *
    **********************************************************************
    */

    $firstlevel = "3000";
    $secondlevel = "7000";
    $thirdlevel = "15000";
    $forthlevel = "28000";
    $fifthlevel = "45000";
    $sixthlevel = "80000"; 

    if($point <= $firstlevel) {

        $total = ($point/$firstlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;

    } elseif ($point <= $secondlevel) {

        $total = ($point/$secondlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $thirdlevel) {

        $total = ($point/$thirdlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $forthlevel) {

        $total = ($point/$forthlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $fifthlevel) {

        $total = ($point/$fifthlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    } elseif ($point <= $sixthlevel) {

        $total = ($point/$sixthlevel) * 100;
        $remaining =  round($total);

        //echo number_format($remaining, 0, '.', ' ');
        return $remaining;
    }


}
  • 写回答

7条回答 默认 最新

  • dongpo8250 2015-08-07 07:16
    关注

    Try this:

    function to_next_level($point) {
    
        /*
        **********************************************************************
        *
        *   This function check how much points user has achievents and how much procent it is until next level
        *
        **********************************************************************
        */
    
        $levelArray = array(3000, 7000, 15000, 28000, 45000, 80000);
        foreach ($levelArray as $level) 
        {
            if ($point <= $level) {
                $total = ($point/$level) * 100;
                $remaining =  round($total);  
    
                //echo number_format($remaining, 0, '.', ' ');
                return $remaining;            
            }
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了