Page 1 of 2
simple arithmetic question
Posted: Wed Aug 04, 2004 8:08 pm
by jamie
Okay, I'm feeling pretty stupid at the moment as I'm sure there is a simple way to do this, but I just can't think of it.
I'm trying to perform an addition inside a select menu with:
echo "<option value='$nextGradClass+5'>7</option>";
where $nextGradClass has a numerical value defined earlier, of say 2006.
However, it's not getting evaluated server side and
<option value='2006+5'>7</option>
is instead being sent to the client.
Please chastise me, but give me a clue too
Posted: Wed Aug 04, 2004 8:12 pm
by nigma
there a few ways you can do it, all fairly simple.
Code: Select all
echo '<option value="' . $nextGradClass+5 . '">7</option>';
Posted: Wed Aug 04, 2004 8:28 pm
by jamie
Well that looks good, but it don't work!
Posted: Wed Aug 04, 2004 8:32 pm
by feyd
($nextGradClass + 5)
Posted: Wed Aug 04, 2004 8:34 pm
by jamie
You are a true saint
Thanks man.
Posted: Wed Aug 04, 2004 8:35 pm
by nigma
sorry.
this will work, but the other example should too.
Code: Select all
$newGradClass = $nextGradClass + 5;
echo "<option value="$newGradClass">7</option>";
Posted: Wed Aug 04, 2004 8:35 pm
by nigma
Hey feyd, help me out, I'm freakin' embarassed.
Why do you have to have parenthesis?
Posted: Wed Aug 04, 2004 8:37 pm
by jamie
Remember that BODMAS stuff at school - guess it forces the parenthesis to be evaluated first.
Posted: Wed Aug 04, 2004 8:40 pm
by nigma
Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction. In that order. It was at least that way where I went to school, but i'm not omnipotent so I could be forgetting something.
In math the statements x + 5 and (x + 5) are the same.
But I guess I don't understand something in php.
Posted: Wed Aug 04, 2004 8:40 pm
by jamie
Thanks for your ideas nigma - had the multiple line version working (too easy), but wanted to tidy up the code into one line as I have 6 of these lines. Guess I could tidy it further, but I'd rather drink a beer. Cheers

Posted: Wed Aug 04, 2004 8:41 pm
by jamie
True, but
2 * x + 5 ain't the same as 2 * (x + 5)
Might be something similar...
Posted: Wed Aug 04, 2004 8:45 pm
by feyd
yeah, it has to do with operator precedence...
Posted: Wed Aug 04, 2004 8:45 pm
by nigma
Haha, this was posted in response to jamie's last post, but didn't post in time.
Yea, could be ?
Anyway, glad you got things worked out, give me a few minutes and I won't be making the same mistake again.
Posted: Wed Aug 04, 2004 8:45 pm
by tim
by changing the value of the variable, you make it an expression
requring it to be enclosed in parenthesis for it to be processed.
Posted: Wed Aug 04, 2004 8:47 pm
by nigma
Jesse, I'm so freakin' embarassed. I'm leaving this forum for the night so I can go memorize my php book.
night
