Addition in PHP?

salukigirl

New Member
Messages
78
Reaction score
0
Points
0
I'm making a game and the animals have randomized stats, 4 of them. I want those to be added together for a "total" and then have the total displayed but how do I do that?
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
so the animals will have unique random stats..or will they change everytime the user logs in?
 

salukigirl

New Member
Messages
78
Reaction score
0
Points
0
they are given the randomized stat when they are created, after that they can't be changed.
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Just run this to add them and display the total, replacing $stat1 - 4 with the variables you're using:

Code:
$total = $stat1 + $stat2 + $stat3 + $stat4;
echo $total

-Luke.
 

mattura

Member
Messages
570
Reaction score
2
Points
18
assuming you get the stats from a database:-
PHP:
//looping through the database on $k: {
$total[$k]=$stat0[$k]+$stat1[$k]+$stat2[$k]+$stat3[$k];
//}
echo sort($total,SORT_NUMERIC); //sort the totals
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
assuming you get the stats from a database:-
PHP:
//looping through the database on $k: {
$total[$k]=$stat0[$k]+$stat1[$k]+$stat2[$k]+$stat3[$k];
//}
echo sort($total,SORT_NUMERIC); //sort the totals

This is perfect example.
PHP:
<?php
foreach ($total as $key => $val) {
    echo "total[" . $key . "] = " . $val . "\n";
}
?>
 
Top