code problems

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

code problems

Post 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*
Attachments
php files.zip
(1.69 KiB) Downloaded 11 times
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code problems

Post by requinix »

There's not much code. Stick it all in your post - just the PHP files.

Where does your checkGrade function end?
p_sha85
Forum Commoner
Posts: 30
Joined: Sat Mar 21, 2009 1:55 pm

Re: code problems

Post 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*
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code problems

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

Re: code problems

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code problems

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

Re: code problems

Post 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>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code problems

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

Re: code problems

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

Re: code problems

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