Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

PHP: Calling a function and passing a value to a variable?

I need to add a statement to the end of this script that calls the checkgrade() function and passes to it the value from the grade form variable. I have the form etc already programmed out, and I have a rough idea how to do this but I'm a little confused if I am on the right path or not. Here is my code, and how I think the statement should be written, but I would like another opinion or two before I try and finalise it on the server:

<?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.";

}

?>

statement:

function checkGrade($_GET["Grade"]

I have an HTML form I am using that has the Get action from this php, with a single text input named Grade.

Update:

oops sorry missed a bracket on the end of the statement there.

Update 2:

The code validates according to W3C.

Update 3:

rb, I get an error in line 34 saying incorrect parsing.

I edited it to this:

function checkGrade($_GET["Grade"])// Call the function

$Grade = $_GET["Grade"];//pass the value from the form variable

echo "<p>$Grade</p>";

which then gave me an error in line 33.

Update 4:

Ok its now telling me it is expecting a ")" in line 30, everything else is right but I don't seem to be missing a bracket, what am I missing?

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Unfortunately, I did not check the code before I posted the first time. I had to add the "case" keyword for option C and drop the function keyword where I called the checkGrade function. This was easy enough to see when I copied the code into Netbeans. If you don't already use Netbeans, you might want to give it a try. See source link (there is a specific download version for PHP).

    <?php

    function checkGrade($Grade) {

    switch ($Grade) {

    case "A":

    $msg = "Your grade is excellent.";

    break;

    case "B":

    $msg = "Your grade is good.";

    break;

    case "C":

    $msg = "Your grade is fair.";

    break;

    case "D":

    $msg = "You are barely passing.";

    break;

    case "F":

    $msg = "You failed.";

    break;

    default:

    $msg = "You did not enter a valid letter grade.";

    break;

    }

    return $msg;

    }

    // *** Now call your function.

    if (isset($_GET["Grade"])) {

    $grade = $_GET["Grade"];

    echo(checkGrade($grade));

    } else {

    echo("Error. No grade selected.");

    }

    ?>

    Source(s): Netbeans IDE: http://netbeans.org/features/php/
  • 1 decade ago
Still have questions? Get your answers by asking now.