Need help figuring out my syntax error problem

hello everyone,

I have currently studying php. I wrote this small code, but I can figure out where I messed up at. I have looked through the code multiple times. I still cannot see where I went wrong. I was hoping for a fresh pair of eyes to help me see the problem.
[php]

<?php if (!isset($_POST['gasprice']) || !is_numeric($_POST['gasprice'])) { echo "Doh!! Please use numbers only ex. 1,2,3."; } else { $gasprice = $_POST['gasprice']; if ($gasprice >= 2 && $gasprice <= 3) { echo "

Ugh!! I guess this $" . $gasprice . "will do..

"; ] elseif ($gasprice < 2) [ echo "

Hallelujah!! And the lord giveth a great gas price!!

"; ] else { echo "What the!?! Better call the police. You bout to get robbed at the gas pump"; ] ?>

[/php]

i made some changes it should work now

[php]

<?php if (!isset($_POST['gasprice']) || !is_numeric($_POST['gasprice'])) { echo "Doh!! Please use numbers only ex. 1,2,3."; } else { $gasprice = $_POST['gasprice']; if (($gasprice >= 2) && ($gasprice <= 3)) { echo "

Ugh!! I guess this $" . $gasprice . "will do..

"; } elseif ($gasprice < 2) { echo "

Hallelujah!! And the lord giveth a great gas price!!

"; } else { echo "What the!?! Better call the police. You bout to get robbed at the gas pump"; } } ?>

[/php]

you were using [] for opening and closing block quotes.

Use curly braces isset or !isset = not set and not numeric ?

[php]
if (isset($_POST[‘gasprice’]) && is_numeric($_POST[‘gasprice’])) {
echo “Doh!! Please use numbers only ex. 1,2,3.”;
} else {
$gasprice = $_POST[‘gasprice’];
if ($gasprice >= 2 && $gasprice <= 3) {
echo “

Ugh!! I guess this $” . $gasprice . “will do…

”;
}elseif ($gasprice < 2) {
echo "

Hallelujah!! And the lord giveth a great gas price!!

";
} else {
echo “What the!?! Better call the police. You bout to get robbed at the gas pump”;
}
[/php]

are you getting any errors?
try echoing the variable gasprice to make sure its getting values

I do not understnd your logic here.
Try this
[php]

$_POST[‘gasprice’] = ‘1’;
if (isset($_POST[‘gasprice’]) && is_numeric($_POST[‘gasprice’])) {
$gasprice = $_POST[‘gasprice’];
if (($gasprice >= 2) && ($gasprice <= 3)) {
echo “

Ugh!! I guess this $” . $gasprice . " will do…

";
} elseif ($gasprice < 2) {
echo "

Hallelujah!! And the lord giveth a great gas price!!

";
} else {
echo “What the!?! Better call the police. You bout to get robbed at the gas pump”;
}
 } else {
           echo "Doh!! Please use numbers only ex. 1,2,3.";

}
[/php]

He is saying if gasprice is not set or gasprice is not numeric carry on so one or the other does not matter will be ok

It should be if gasprice is set AND is numeric then go not one or the other but both have to be correct before carrying on

[php]
if (!isset($_POST[‘gasprice’]) || !is_numeric($_POST[‘gasprice’]))
[/php]

the else statement should still work if gas price is not set or is not numeric. but i totally agree with you noodles your codes are more logical i would done it the same way

Noodles,

The php would be part of a form. I didn’t post that part dues to there being any errors. My apologies if I let anyone confused. What the php should do is when inputting a numeric value into the form depending on the number it should echo out one of the statements. For example if inputting the numeric value 1 the echoed statement should be “Hallelujah!! the lord giveth us a great gas price.” this is what I am attempting with this. I am just starting off with small scripts to understand php before moving on to larger scripts.

You did not confuse me just trying to help you understand, why we would code it the other way instead of your way around it saves 2 chars :stuck_out_tongue:

ok I have been trying to figure out why the echos are not changing when different value are placed in the form.
what it should be doing is if the form is left blank or a value other than a numerical value is entered it should say " please use number only ex. 1,2,3"
then if the numerical value is greater than 2 but less than 3 then it should say “ugh!! i guess $ will do”
but if the numerical value is less than 2 then it should say" hallelujah!! god giveth us a great gas price"
or if the numerical value is greater than 2 then it should say the final echo.
My problem is the echo statements are not changing.
question, could wamp be causing this problem? im not receiving errors anymore. before when i had the problems with the syntax errors I changed the settings as i was getting a 403 forbidden error.

Sounds like there is a problem alright as I checked the code was working and I am sure wilson did as well.
at the top of the page do you have any error mesage changes like

error_reporting( ********);

or could be in the php.ini file you changed it in wamp.
But if it does not echo the messages then there is a problem somewhere.

Noodles,

  I am not receiving any errors in both my GUI editor or wamp. The only problem I am getting is that the first initial echo stays up and does not change when the value changes. I will check how to change my wamp setting back to default or possibly re-install if not able to do and see if this rectifies the problem.

So you just see the error message all the time
[php]echo “Doh!! Please use numbers only ex. 1,2,3.”;[/php]

maybe you need to trim your post value
echo out your post value something like

[php]echo ‘’.$_POST[‘gasprice’].’’;[/php]

see what the result is if you have white space trim it off
[php]trim($_POST[‘gasprice’]);[/php]

if no value is set then come back your form maybe wrong

This is what the whole code looks like

<div>
<center>
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Gas Price $ <input type="text" name"gasprice" /> <input type="submit" value="Enter"/>
</form>


<?php
if (!isset($_GET['gasprice']) || !is_numeric($_GET['gasprice'])) 
{
    echo "Please use numbers only ex. 1,2,3.";
} else 
{
    $gasprice = $_GET['gasprice'];
    
     if ($gasprice >= 2) {
	  if ($gasprice <= 3) 
	 
       echo "<p>Ugh!! I guess this $" . $gasprice . "will do..</p>";
	} elseif ($gasprice < 2) 
	{
        echo "<p> Hallelujah!! And the lord giveth a great gas price!!</p>";
    } else 
	{
        echo "What the!?! Better call the police. You bout to get robbed at the gas pump";
	}
}
?>
</center>
</div>

Did you try the way I coded the same thing in the posts before?

Your logic is not correct this is why it shows the error message, that is why I corrected your logic.

!isset = is not set which is correct it is not sett show error
!is_numeric = is not numeric show error

So as soon as you get on the page the error will be there.

also you should be using POST rather then GET in your form

or try changing

input type="text" name"gasprice"

to
[php]input type=“text” name=“gasprice”[/php]

Noodles,

You are the best!!! yes that was my problem I forgot to add the = to name in the form.. If I ever see you I'm giving you a high five!!

Noodles and Wilson,

I really truly appreciate this. I had bout burn my brain out trying to figure this out. :P Just shows how one little piece missing from your code can mess up the whole thing. Is there anyway I can show my appreciation by helping your karma go up? You two are the best!!

Well atleast you are happy and can carry on coding and learning more.

click the solved and the karma if you want to.

Sponsor our Newsletter | Privacy Policy | Terms of Service