[SOLVED] PHP Math

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
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

[SOLVED] PHP Math

Post by bawla »

i made this code to figure out some calculations

Code: Select all

<?php
$value1=$_GET&#1111;'value1'];
$value2=$_GET&#1111;'value2'];
$value3=$_GET&#1111;'value3'];
$value4=$_GET&#1111;'value4'];
$total1=($value1 + $value2 + $value3 / $value4);
echo ($total);
?>
but it doesnt seem to work, this is for a form that is in html

if u want to see an example http://www.krunkdesigns.com/gtthing/gtthing.html
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

echo ($total);

should be

echo ($total1);

Just a typo

Mark
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: PHP Math

Post by TheBentinel.com »

bawla wrote:

Code: Select all

$total1=($value1 + $value2 + $value3 / $value4);
echo ($total);
Mark's right, you wanted $total1 there.

But also, I'm not sure about the precedence of +'s and /'s. To be safe, you should probably wrap parentheses around the parts you want done first, like:
$total1=( ($value1 + $value2 + $value3) / $value4);
or
$total1=($value1 + $value2 + ($value3 / $value4) );

I'm not sure what you're after so I don't know where they belong. Even if only for clarity of code, I'd stick them in.


but it doesnt seem to work, this is for a form that is in html

if u want to see an example http://www.krunkdesigns.com/gtthing/gtthing.html
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ya, order of operations would divide $value3 by $value4, then do the addition.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

i know its not vital

but in your scripts you should add some clauses as i went straight to the link and just clicked the submit button and i got a nasty error message (something you dont want users to see)

Code: Select all

if
(isset($variablename))
{
// this var is set
}
else
{
// this var is not set so we'll do something
exit();
}
just some food for thought
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

thanks you guys i got it to work now
Post Reply