Page 1 of 1
switch statement error
Posted: Wed Apr 22, 2009 9:59 pm
by p_sha85
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!!
Code: Select all
<!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>
Re: switch statement error
Posted: Wed Apr 22, 2009 10:41 pm
by reinerlee
your $Grade is NULL before calling checkGrade(),
and you are calling checkGrade($Grade),
thus there is not matching with ABCDF.
Add in this before you call function checkGrade();
$Grade = $_GET["grade"];
OR
echo "<p>Grade:", $_GET["grade$Grade"], checkGrade($Grade), "</p>";
Re: switch statement error
Posted: Wed Apr 22, 2009 11:10 pm
by p_sha85
Okay, how do I make it not NULL and where do I do that? $Grade is in several different places in the code... let me know.. thanks!
Re: switch statement error
Posted: Wed Apr 22, 2009 11:34 pm
by liljester
change
Code: Select all
echo "<p>Grade:", checkGrade($Grade), $_GET["grade$Grade"], "</p>";
to
Code: Select all
echo "<p>Grade:", checkGrade($_GET["grade$Grade"]), $_GET["grade$Grade"], "</p>";
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..
Re: switch statement error
Posted: Wed Apr 22, 2009 11:44 pm
by p_sha85
ahhh that was the problem! it's fixed now =D thanks a ton to both of you!!!
Re: switch statement error
Posted: Thu Apr 23, 2009 9:15 am
by dethron
* 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
Re: switch statement error
Posted: Thu Apr 23, 2009 9:31 am
by mis
When you use “ ” server looks every variable in it but if you use ‘ ’ server won’t look for what is written there.
echo 'Grade:'.checkGrade($grade) would be simplier to call the function and print the sentence
Re: switch statement error
Posted: Thu Apr 23, 2009 9:50 am
by mis
Code: Select all
<?
function checkGrade($grade) {
switch ($grade) {
case "A":
$result = 'Your grade is excellent.';
break;
case "B":
$result = 'echo "Your grade is good.';
break;
case "C":
$result = 'Your grade is fair.';
break;
case "D":
$result = 'You are barely passing.';
break;
case "F":
$result = 'You failed.';
break;
case NULL:
$result = 'enter something';
break;
default:
$result = 'You did not enter a valid letter grade.';
break;
}
return $result;
}
?>
<!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>
<?
$test = 5;
echo "My test value is $test.<br />";
echo 'My test value is '.$test.'<br />';
?>
<form action="grade.php" method="post">
<input name="grade" type="text" size="5" />
<input name="submit" type="submit" value="Submit" />
</form>
<?php
$grade = $_POST["grade"] ;
echo 'Grade:'.checkGrade($grade).'<br />';
?>
</body>
</html>