Help with Array Mathematics

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
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Help with Array Mathematics

Post by mhouldridge »

Hi,

I need to add up variables as follows;

$oscost
$cpucost
$hddcost

and so on....

I cant for the life of me remember how to perform math functions...

Please help.

thanks in advance.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hmmm...

Code: Select all

<?php

$total = $oscost + $cpucost + $hddcost;

?>
Last edited by JayBird on Tue Jan 31, 2006 5:16 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

:?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

If it's an array you're adding up then use array_sum().

Code: Select all

<?php

$array = array(1, 2, 4, 8, 16);

echo array_sum($array); //31

?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Pimptastic wrote:hmmm...

Code: Select all

<?php

$total = $oscost + $cpucost + $hddcost;

?>
did this not work?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

there you go, that should solve you problem if you didnt come across it already.

let us know how you made out.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Mathmaticians do it on matrices ... :roll:
(#10850)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I am very curious to know what the question is...is it just about addition of numbers... :roll:
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I would assume it is numbers, because his variables have "cost" in it. I am not %100 positive though.
Post Reply