simple arithmetic question

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

jamie
Forum Newbie
Posts: 7
Joined: Sun Nov 23, 2003 11:41 pm

simple arithmetic question

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

there a few ways you can do it, all fairly simple.

Code: Select all

echo '<option value="' . $nextGradClass+5 . '">7</option>';
jamie
Forum Newbie
Posts: 7
Joined: Sun Nov 23, 2003 11:41 pm

Post by jamie »

Well that looks good, but it don't work!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

($nextGradClass + 5)
jamie
Forum Newbie
Posts: 7
Joined: Sun Nov 23, 2003 11:41 pm

Post by jamie »

You are a true saint :)

Thanks man.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

sorry.

this will work, but the other example should too.

Code: Select all

$newGradClass = $nextGradClass + 5;
echo "<option value="$newGradClass">7</option>";
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Hey feyd, help me out, I'm freakin' embarassed.

Why do you have to have parenthesis?
jamie
Forum Newbie
Posts: 7
Joined: Sun Nov 23, 2003 11:41 pm

Post by jamie »

Remember that BODMAS stuff at school - guess it forces the parenthesis to be evaluated first.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Last edited by nigma on Wed Aug 04, 2004 8:41 pm, edited 1 time in total.
jamie
Forum Newbie
Posts: 7
Joined: Sun Nov 23, 2003 11:41 pm

Post 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 :)
jamie
Forum Newbie
Posts: 7
Joined: Sun Nov 23, 2003 11:41 pm

Post by jamie »

True, but

2 * x + 5 ain't the same as 2 * (x + 5)

Might be something similar...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah, it has to do with operator precedence...
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Last edited by nigma on Wed Aug 04, 2004 8:46 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

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