Page 1 of 1

code problems

Posted: Tue Apr 21, 2009 1:45 pm
by p_sha85
Okay, so I'm having some trouble implementing these codes. I keep getting error messages for them saying that there is some parse/syntax error.. ?? When I use an HTML validator, it doesn't find any errors so I'm not sure why this is not working. If someone can take a look at the code for me and let me know what I'm doing wrong I will really appreciate it.. file is attached. thanks a ton!!


*Pooja*

Re: code problems

Posted: Tue Apr 21, 2009 2:34 pm
by requinix
There's not much code. Stick it all in your post - just the PHP files.

Where does your checkGrade function end?

Re: code problems

Posted: Tue Apr 21, 2009 2:51 pm
by p_sha85
Okay, here are the code files:

Gas Prices code:

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>Gas Prices</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
echo "<p>Gas price: ", $_GET["price"], "</p>";
$GasPrice = 1.57;
if ($GasPrice >=1) && ($GasPrice <=2)
    echo "<p>Gas prices are between
    $1.00 and $2.00.</p>";
else 
    echo "<p>Gas prices are not between
    $1.00 and $2.00.</p>";
?>
</body>
</html>
 


Letter Grades code:

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.";
    }
}
echo "<p>Grade checkGrade(): ", $_GET["grade$Grade"], "</p>";
?>
</body>
</html>
 
For the gas prices code, it should tell 'gas prices are between $1 and $2' or 'are not between $1 and $2' based on whatever the user enters in the text box. I'm not getting that answer.. it gives some error. For the letter grades code, it should tell the echo statement under each grade if that grade is input into the box but I'm not getting that phrase either. If someone can tell me where I'm going wrong then thanks a ton!


*Pooja*

Re: code problems

Posted: Tue Apr 21, 2009 3:01 pm
by requinix
I'm not seeing the problem with the gas one. What error do you get?

For the grades, you don't call checkGrade anywhere. It can't do anything unless you call it.
The function needs to return a string, not echo it. Otherwise stuff will appear out of order.

Re: code problems

Posted: Tue Apr 21, 2009 3:08 pm
by p_sha85
Ohhh ok , thanks for the suggestion on the checkGrades one.. will fix that.

The gas prices one gives this error from the server its hosted on: Parse error: syntax error, unexpected T_BOOLEAN_AND in /home/a5936353/public_html/GasPrices.php on line 11

Re: code problems

Posted: Tue Apr 21, 2009 3:26 pm
by requinix
Oh. Right. :oops:

Code: Select all

if (condition)
The entire condition has to be inside a set of parentheses, but you can use more of them if you want.

Code: Select all

if (($GasPrice >=1) && ($GasPrice <=2))

Re: code problems

Posted: Tue Apr 21, 2009 5:32 pm
by p_sha85
Yaay, that fixed the gas prices problem! Thanks a ton

Otherwise, I thought I had fixed the letter grades problem but now i'm getting the following error: Parse error: syntax error, unexpected T_STRING in /home/a5936353/public_html/LetterGrades.php on line 30

I don't know what that means... O_o Please see the corrected (?) code below:

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.";
    }
};
return "<p>Grade" checkGrade()": " $_GET["grade$Grade"] "</p>";
?>
</body>
</html>
 

Re: code problems

Posted: Tue Apr 21, 2009 6:21 pm
by requinix
Earlier you had commas to separate everything. They were good.

Now you lost them. How about putting them back?

Also, return is only valid inside a function. What you had before is closer to what you need - just a couple adjustments, like making the function return a string instead of directly printing it.

Re: code problems

Posted: Tue Apr 21, 2009 6:29 pm
by p_sha85
Okay, I put the commas back and I'm still getting this error: Parse error: syntax error, unexpected T_STRING in /home/a5936353/public_html/LetterGrades.php on line 30

Here is the line where I put the commas.. let me know if I'm doing it wrong:

Code: Select all

 
return "<p>Grade" checkGrade(), ": " $_GET["grade$Grade"], "</p>";
 
I just noticed you said not to put return... I guess I don't know what to put there...

Re: code problems

Posted: Wed Apr 22, 2009 11:56 am
by p_sha85
Okay I fixed one problem I guess because I'm no longer getting an error. Now though, the code is just not doing what I want it to do. It keeps returning the default value of the switch statement no matter what I put into the textbox. Even if I put in a grade which is defined in the switch statement, it will not return the value for that grade; only the default value. Do I need to do something else to get it to work right? Let me know where I'm going wrong.. 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>