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

    ?>

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

Your array elements are in string .convert all element to int using

    gradeArray($x){
$score=(int)$x;
}

Try this it will work

First off, most likely $score is undefined in your function since you use $x.

function gradeArray($x){

Then you are using your if conditions as if($score>= 70){.

Also, in your return values, just use return.

return"A"; // and others

Use return not echo so that this concatenation echo "<td>". gradeArray($score); works.

$score is undefined in your variable, the function is fetching the variable as $x try to change $score to $x or define $score., you can use global $score also in your function. Also you should return the values instead of using echo use the code below

<?php

//function intended to  grade array elements

    function gradeArray($x){
$score=$x;
        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>";



      echo$score."</td>";

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

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



      }



       echo"</table>";

    ?>

Hope this helps you

Try this

<?php

//function intended to  grade array elements

function gradeArray($x) {
    if ($x >= 70) {
        return "A";
    } elseif ($x >= 50) {

        return "B";
    } elseif ($x >= 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>";



    echo$score . "</td>";

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

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



echo"</table>";
?>
function grading( $marks ){
    $grade = mysql_query("SELECT grade_name,grade_point FROM table_name **strong text**WHERE smark <= round($marks) AND hmark >= round($marks)");

     $gd = mysql_fetch_row( $grade );

     return $gd[0];
}