spfoonnewb Posted April 7, 2007 Share Posted April 7, 2007 I have been working on this for a while.. and cannot figure it out. It currently outputs 1 +1 +1 +1 +1 +1 +1 +1 +1 +1 + I want it to add them together.. <?php for ($xz = 1; $xz <= 10; $xz++) { $times[] = "1"; } foreach ($times as $tt) { $var[] = "$tt"; } foreach ($var as $tt2) { echo "$tt2 +"; } ?> Link to comment https://forums.phpfreaks.com/topic/46065-adding-and-arrays/ Share on other sites More sharing options...
MadTechie Posted April 7, 2007 Share Posted April 7, 2007 you mean like this ? <?php foreach ($var as $tt2) { echo "$tt2 +"; } ?> to <?php $total = 0; foreach ($var as $tt2) { $total = $total + $tt2; } echo $total; ?> Link to comment https://forums.phpfreaks.com/topic/46065-adding-and-arrays/#findComment-223821 Share on other sites More sharing options...
spfoonnewb Posted April 7, 2007 Author Share Posted April 7, 2007 Ok that adds them together, but how can I make a string that I can use later in an if statement? I want amt, to be equal to the values from the array.. but appear the way I have it below. Example: <?php $amt = "1 + 1 + 1 + 1 + 1 + 1 + 1"; if ($total = $amt) { //Do something } ?> Link to comment https://forums.phpfreaks.com/topic/46065-adding-and-arrays/#findComment-223823 Share on other sites More sharing options...
Barand Posted April 7, 2007 Share Posted April 7, 2007 <?php for ($xz = 1; $xz <= 10; $xz++) { $times[] = "1"; } $sum = array_sum($times); ?> Link to comment https://forums.phpfreaks.com/topic/46065-adding-and-arrays/#findComment-223841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.