douzhan4522 2015-01-14 06:50
浏览 59
已采纳

使用php函数对数字索引数组的元素进行分级

I intend to grade the elements of a numerically indexed array in php using function call; my function call does not work: it doesn't grade the elements and I cannot figure out the error in the code.Please help Thanks, in advance

 <?php

//function intended to  grade array elements

    function gradeArray($x){
        if($score>= 70){
    echo"A";


     }


       elseif($score >= 50){


     echo"B";
    }
    elseif($score>= 40){


     echo"C";


     }


     else{
    echo"F";


     }
    }

    // Array of Scores to be graded


      $scores = array ("55", "68", "43", "78");
//Display result in a tabular form


     echo"<table  border = '1'><th>Score</th><th>Grade</th>";

    foreach($scores as $score){



     echo"<tr><td>";



      echo$score."</td>";

    echo"<td>". gradeArray($score);

    echo"</td></tr>";



      }



       echo"</table>";

    ?>
  • 写回答

6条回答 默认 最新

  • douwei1930 2015-01-14 07:54
    关注

    You are passing $x into your function then calling $score. Your scores array is also in string format, just need to remove the quotes to make them numbers. Also change $x to $score and it should work fine! :)

    <?php
    //function intended to  grade array elements
    function gradeArray($score) {
       if     ($score >= 70)  return "A";
       elseif ($score >= 50)  return "B";
       elseif ($score >= 40)  return "C";
       else                   return "F";
    }
    
    // Array of Scores to be graded
    $scores = array (55, 68, 43, 78);
    
    //Display result in a tabular form
    echo "<table border='1'><th>Score</th><th>Grade</th>";
    
    foreach ($scores as $score) {
        echo "<tr><td>$score</td><td>" . gradeArray($score) . "</td></tr>";
    }
    
    echo "</table>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动