PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Can someone please tell me what I'm doing wrong in this code? It keeps only returning the default value in the switch statement, not the individual values. Please take a look at the code and let me know what the error is.. thanks!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Letter Grades</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
function checkGrade($Grade) {
switch ($Grade) {
case "A":
echo "Your grade is excellent.";
break;
case "B":
echo "Your grade is good.";
break;
case "C":
echo "Your grade is fair.";
break;
case "D":
echo "You are barely passing.";
break;
case "F":
echo "You failed.";
break;
default:
echo "You did not enter a valid letter grade.";
break;
}
};
echo "<p>Grade:", checkGrade($Grade), $_GET["grade$Grade"], "</p>";
?>
</body>
</html>
i think thats what reinerlee meant to suggest after the "OR".
the problem is you were passing a null value $Grade to your function, becasue $Grade had not yet been defined anywhere in your code. reinerlee's first suggestion would remedy that. or you could take the second suggestion and just pass $_GET variable to your function.
are you intentionally using $_GET["grade$Grade"] ? it looks like a typo, unless you are passing a $Grade variable into index. what is in your $_GET[""] should be the name of your form element you are passing... lol i hope that didnt just confuse you more..
* Variables with starting with big letters => not seem nice to me * " used when ' does the job => server does not like it * composed variable names ($_GET["grade$Grade"]) => unnecessary complexity