使用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>";
?>
我打算使用函数调用在php中对数字索引数组的元素进行分级; 我的函数调用不起作用:它没有对元素进行分级,我无法弄清楚代码中的错误。请帮助 谢谢,提前 p>
&lt;?php
//用于对数组元素进行分级的函数
函数gradeArray($ x){
if($ score&gt; = 70){
echo“A”;
}
elseif($ score&gt; = 50){
echo“B”;
}
elseif($ score&gt; = 40){
echo“C”;
}
其他{
echo“F”;
}
}
//要评分的分数数组
$ scores = array(“55”,“68”,“43”,“78”);
//以表格形式显示结果
echo“&lt; table border ='1'&gt; &lt; th&gt;得分&lt; / th&gt;&lt; th&gt;等级&lt; / th&gt;“;
foreach($得分为$得分){
回声”&lt; tr&gt;&lt; td&gt; ;“;
echo $ score。”&lt; / td&gt;“;
echo”&lt; td&gt;“。 gradeArray($ score);
echo“&lt; / td&gt;&lt; / tr&gt;”;
}
echo“&lt; / table&gt;”;
?&gt;
code> pre>
div>
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];
}