使用php函数对数值索引数组的元素进行分级
我打算使用函数调用对php中数字索引数组的元素进行分级;我的函数调用不起作用:它没有对元素进行分级,我无法找出代码中的错误。请帮助 提前谢谢使用php函数对数值索引数组的元素进行分级,php,Php,我打算使用函数调用对php中数字索引数组的元素进行分级;我的函数调用不起作用:它没有对元素进行分级,我无法找出代码中的错误。请帮助 提前谢谢 <?php //function intended to grade array elements function gradeArray($x){ if($score>= 70){ echo"A"; } elseif($score >= 50){ ec
<?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>";
?>
数组元素为字符串。请使用将所有元素转换为int
gradeArray($x){
$score=(int)$x;
}
试试这个,它会起作用的。首先,由于使用了$x,很可能函数中未定义$score
然后使用if条件,就好像$score>=70{
另外,在返回值中,只需使用return
使用return not echo,以便此连接echo.gradeArray$score;工作。$score在变量中未定义,函数将变量提取为$x。尝试将$score更改为$x或定义$score。您也可以在函数中使用全局$score。此外,您应该返回值,而不是使用echo。请使用下面的代码
<?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>";
?>
希望这对你有所帮助试试这个
<?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>";
?>
您将$x传递到函数中,然后调用$score。 您的分数数组也是字符串格式,只需删除引号,使其成为数字。 同时将$x更改为$score,它应该可以正常工作!:
<?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>";
?>
<?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>";
?>
<?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>";
?>
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];
}