Add up numbers in a for each PHP? - php

I'm trying to add some numbers in a foreach loop in my PHP code. I am getting a percentage of numbers in a while loop in my MySQL query for each result that I get in my PHP page.
All I need to do is to add up the final values in and show them as total.
This is how I make up the percentage in my while loop in my MySQL query:
$percentage = 10;
$totalWidth = $fees;
$new_width = ($percentage / 100) * $totalWidth;
The $fees value is dynamic and it is different for each result in my while loop. the code above works as it should.
Now I want to add up all the values of $new_width. For example:
If one result's $new_width is 25 and the other one is 10 and another one is 5, I need to do this: $total = 25 + 10 + 5;
So I tried something like this:
$total = 0;
foreach($new_width as $var) {
$total = $var + $var;
}
echo $total;
but the above code doesn't really make sense and it won't do anything at all.
Could someone please advise on this matter?

First you want to change this line in your while loop so you get an array:
$new_width = ($percentage / 100) * $totalWidth;
to this:
//Declare it as an array before your while loop
$new_width = array();
//In your while loop
$new_width[] = ($percentage / 100) * $totalWidth;
//^^ See here
After this you just have to change the line in your foreach loop like this:
$total = $var + $var;
to this:
$total += $var;
(If you want you also can do this in your while loop)

If you have an array of numbers and you want to calculate the sum of those numbers, you should use array_sum().

According to the logic, you are setting the total to 2 X $var.
My answer is very similar, but you add it to the total which is outside of the loop and the value will keep growing:
$total = 0;
foreach($new_width as $var) {
$total += $var;
}
echo $total;
Or simply as stated before, if it is the only value in the array:
$total = array_sum($new_width);

Related

SUM with loop in PHP

i want to SUM number using loop in php. And here's my code
$point = 0,3;
for ($i=1;$i<=40;$i++){
$total = $point + $i;
}
echo $total;
But when I run the program, it print wrong result. It printed 40.3, and the right result is 12
The point is, I want to SUM each point until 40 times.
example : 0.3+0.3+0.3+...+0.3 (40 times)
It should be like this
$point = 0.3;
$total = 0;
for ($i=1;$i<=40;$i++){
$total = $total+$point;
}

having trouble to this one answering this While loop

Here's my problem:
Use a while loop to write a program that displays the sum of the perfect squares of numbers 1 to 20. Use the variable $counter as the loop control variable. Create variables named $squares and $sum to hold the results. After and outside the loop body, use echo to display the string The sum of the perfect squares is $sum. The variable $sum should be replaced with its value.
Hint: A perfect square is a number that is the product of an integer multiplied by itself. For example, the perfect square of 2 is 4 since 2*2 = 4.
problem 1: Expected output is "The sum of the perfect squares is 2870."
problem 2: Expecting two occurrences of the variable named 'squares'.
problem 3: Expecting four occurrences of assignment operations that assign the values to the variables 'counter', 'sum', and 'squares'.
here's my code:
<?php
$counter = 1;
while($counter <= 20)
{
$squares = $counter * $counter;
$sum = $squares + $squares;
echo "The sum of the perfect squares is $sum. \n";
$counter ++;
}
?>
<?php
$counter = 1;
$sum = 0;
$squeares = array() ;
while($counter <= 20)
{
$squares[$counter] = $counter * $counter;
$sum += $squares[$counter];
$counter ++;
}
echo "The sum of the perfect squares is ". $sum;
?>
check it

For loop inside a function PHP

A total noob needs to do a math calculation in a for loop inside a function.
The closest I come is this, but it does not work of course.
function z1()
{
for ($x=1; $x<=11; $x++)
{
$z1=2/3+5*$x/(3-$x);
}
return $z1;
}
Any help or a solution would be appreciated
EDIT: FIXED
Needed to declare what to do if it is NaN or infinite
Not entirely sure what you're trying to do, but I'll make some assumptions.
In addition to a divide by zero error this gets, you are overwriting the value of $z1 each time the loop executes, so the value of $z1 once you return is whatever the final iteration of the loop assigned to $z1.
I think you may want to initialize the variable before the loop and use the += operator inside the loop, so the value of $z1 is incremented each time the loop iterates.
function z1() {
$z1 = 0;
for ($x = 1; $x <= 11; $x++) {
$z1 += 2 / 3 + 5 * $x / (3 - $x);
}
return $z1;
}

Find the matched value of an array with a given value

I have an array of value series like:
$series = [100,300,500,800,1000,3000,5000,10000,15000,20000];
Another value getting from DB like:
$point = $data[‘earned_point’];
I need the highest match from the series. such as I got a value from db (1500) the highest match of the value is 1000 in the series, so I need to get the $series[4] and make it
$reward = $series[4] * 0.1;
I'll run it in a loop to do it for all the values got from DB.
I'm posting alternate code as the accepted answer while is correct can be very inefficient if you are working with a large array.
<?php
function computeReward($series, $point, $percent = 0.1){
arsort($series); // sort the series in reverse so we can pass any array and the highest number will be the first because we are looking for a number lower or equal to our point
foreach($series as $min_point){
if($min_point <= $point){
return $min_point * $percent; // return the min_point * the percentage, this stops the loop there for being more efficient
}
}
}
$series = [100,300,500,800,1000,3000,5000,10000,15000,20000];
$point = $data['earned_point'];
$reward = computeReward($series, $point);
?>
Do you mean you want to get which highest $series item is equal or less than $point ?
<?php
$series = [100,300,500,800,1000,3000,5000,10000,15000,20000];
$point = $data['earned_point'];
foreach ($series as $min_point) {
if ($point >= $min_point) {
$matched_min_point = $min_point;
}
}
$reward = $matched_min_point*0.1;
Let me know if this works for you

Using For loop inside function - not returning all values

I am having problems displaying the results of function calculateGrowth. As shown below, I would like to find the results for each day of a 10 day period. I used a for loop for obvious reasons. But when I try to display results, all I get back is one result.
function calculateGrowth(){
$days = 0;
$growth = 10;
for($days = 0; $days < 10; $days++){
$totalGrowth = $growth * pow(2, $days/10);
}
return $totalGrowth;
}
Current Output
18.6606598307
Desired Output
Day Growth
1 - result
. - result
. - result
10
$totalGrowth = $growth * pow(2, $days/10);
Should be
$totalGrowth[] = $growth * pow(2, $days/10);
^^
So that it becomes an array and contains all the values that you add to it, rather than being a string which gets overwritten on every iteration of the loop.
It sounds like what you're trying to get this:
function calculateGrowth() {
list($days, $growth, $table) = array(range(1, 10), 10, array());
foreach ($days as $day) {
$table[$day] = $growth * pow(2, $day/10);
}
return $table;
}
It is correct as your final loop is doing this
$totalGrowth = 10 * pow(2, 9/10)
$totalGrowth = 10*1.8660659830736;
$totalGrowth = 18.660659830736;
Edit : Removed last comment

Categories