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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效