Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do this: $Total= $Total + $Qty... 4

Status
Not open for further replies.

Homersim2600

Technical User
Dec 15, 2004
253
US
Hello,

I am a noobie to PHP migrating from Visual Basic. I know that in VB a programmer can add a number to itself, and then store the result inside of itself all in the same line. My question is, I am able, to some extent, to get PHP to do the math, but then I receive an error, "variable not defined" Is there a 'proper' way to perform the function $a = $a + $b ?

What I am doing is pulling data from a MySQL database and adding all of values from a specified field in the database together to get a grand total. So, is there an alternative way to go about this without having to worry about my main query to you folks?

Thank you all for your insight.

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
In PHP, any time you use an uninitialized variable in a mathematical expression, you'll get that error.

For example, the script:

<?php
$a = $a + $b;
?>

will generate the errors:

PHP Notice: Undefined variable: a in ... on line 2
PHP Notice: Undefined variable: b in ... on line 2


Just initialize your variables:

<?php
$a = 0;
$b = 3;

$a = $a + $b;
?>

before you use them.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
GOstlund:
If $a is uninitialized, you'll still get an:

PHP Notice: Undefined variable: a in ... on line ...

with the += operator

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
A star for you both! Both methods work nicely!

Thank you very much!

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Glad you have a method working for you.

Depending on whether or not you're doing anything else with the data, you may be interested to know that your query can actually do the summation for you if you prefer.

SELECT SUM(columnA) FROM table WHERE clause
 
AHHHHHH HAAAAAA!

WOW! That is really handy! Thank you thank you.

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Btw. I have one more addition:

Some times, a variable might be NULL.
I have too programmed in VB, so I guess you know NULL is != 0.

If you do:
10 + 19 / NULL, it == NULL
etc.

So, you also have to redefine NULL or maybe exclude it, if needed.

I think this issue might arrive when you gather some integers from a function:

$total += getInteger();
if getInteger() there gives NULL...

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top