switch statement error

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!

Moderator: General Moderators

Post Reply
p_sha85
Forum Commoner
Posts: 30
Joined: Sat Mar 21, 2009 1:55 pm

switch statement error

Post 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>
 
reinerlee
Forum Newbie
Posts: 3
Joined: Wed Apr 22, 2009 10:30 pm

Re: switch statement error

Post 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>";
p_sha85
Forum Commoner
Posts: 30
Joined: Sat Mar 21, 2009 1:55 pm

Re: switch statement error

Post 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!
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: switch statement error

Post 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..
p_sha85
Forum Commoner
Posts: 30
Joined: Sat Mar 21, 2009 1:55 pm

Re: switch statement error

Post by p_sha85 »

ahhh that was the problem! it's fixed now =D thanks a ton to both of you!!!
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Re: switch statement error

Post 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
mis
Forum Newbie
Posts: 4
Joined: Sun Apr 12, 2009 9:47 am

Re: switch statement error

Post 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
mis
Forum Newbie
Posts: 4
Joined: Sun Apr 12, 2009 9:47 am

Re: switch statement error

Post 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>
Post Reply