frshjb373 Posted September 1, 2012 Share Posted September 1, 2012 I'm trying to add 2 to the $price each time the variables below isset (contrarily, I don't want to add 2 to the $price for any variable that is empty) . I have pasted my current sample code below. It works with the first statement by itself, but when I add more than one if statement it causes an error. Please advise what the best way to handle this is? I'm sure it's very simple...but I'm missing something. Still a beginner. Thank you in advance for the help! <?php $price = 135; ?> <?php if (isset($field_Charger)) { $price = $price + 2; } if (isset($field_Case)) { $price = $price + 2; } if (isset($field_Software)) { $price = $price + 2; } if (isset($field_Manual)) { $price = $price + 2; } if (isset($field_Box)) { $price = $price + 2; ?> <?php echo $price; ?> Quote Link to comment https://forums.phpfreaks.com/topic/267904-increment-a-value-based-on-if-variables-isset/ Share on other sites More sharing options...
DavidAM Posted September 1, 2012 Share Posted September 1, 2012 It would help if you would tell us what the error is. Or better yet copy & paste it into your post. The only thing I see wrong with that code is a missing right-curly brace after the last if statement. Note: it is not necessary to close and open the PHP blocks (?> <?php) blank lines are allowed in php code. You could save some typing by using $price += 2; Quote Link to comment https://forums.phpfreaks.com/topic/267904-increment-a-value-based-on-if-variables-isset/#findComment-1374567 Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2012 Share Posted September 1, 2012 You're missing a closing curly brace on the last if(){, but there almost has to be a better way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/267904-increment-a-value-based-on-if-variables-isset/#findComment-1374572 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.