Iteration in PHP dynamic variable - php

function stepzero(){
$alcount = $_POST["alcount"];//retrieve number of row
$crcount = $_POST["crcount"];//retrieve number of coloumn
for ($x=1; $x <=$crcount ; $x++) { //Loop for coloumn
for ($y=1; $y <=$alcount ; $y++) { //Loop for row
${"v".$y."t".$x} = $_POST["r".$y."c".$x];//retrieve value of table
${"v".$y."t".$x} = ${"v".$y."t".$x}*${"v".$y."t".$x};//square the value
${"nv".$y."t".$x} = ${"nv".$y."t".$x} + ${"v".$y."t".$x}; //not working
echo ${"nv".$y."t".$x};
echo "<br>";
}
}
}
I have this function to retrieve value from table , square it and then sum it. But ${"nv".$y."t".$x} returns the same value as ${"v".$y."t".$x} (it didn't sum it). How can I fix this?

This code could be a lot simpler.
For example, these two lines:
${"v".$y."t".$x} = ${"v".$y."t".$x}*${"v".$y."t".$x};//square the value
${"nv".$y."t".$x} = ${"nv".$y."t".$x} + ${"v".$y."t".$x}; //not working
Could be better written as:
${"v".$y."t".$x} *= ${"v".$y."t".$x};//square the value
${"nv".$y."t".$x} += ${"v".$y."t".$x}; //not working

If you just want to get the sum of the squares of a bunch of values, try this:
function stepzero(){
$alcount = $_POST["alcount"];//retrieve number of row
$crcount = $_POST["crcount"];//retrieve number of coloumn
$sum = 0;
for ($x=1; $x <=$crcount ; $x++) { //Loop for coloumn
for ($y=1; $y <=$alcount ; $y++) { //Loop for row
$sum += $_POST["r".$y."c".$x] * $_POST["r".$y."c".$x];
}
}
return $sum;
}

Related

PHP Add numbers 1-10 and only even numbers 1-10. Display sums separately with one loop

I'm trying to use ONE PHP for loop to echo the sum of the numbers 1-10 as well as echo the sum of only the even numbers. I seem to have a problem as these iterations won't be "parallel"
Code:
<?php
$sum = 0; $evensum = 0;
for($x = 1, $y=2; $x<=10, $y<=6; $x++, $y += 2) {
$sum = $sum + $x; $evensum = $evensum + $y;
}
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
total sum should reflect 55 (1+2+3+4+5+6+7+8+9+10) and even sum should reflect 30 (2+4+6+8+10)
Just Use
<?php
$sum = 0; $evensum = 0;
for($x = 1; $x<=10; $x++) {
// sum all the number
$sum = $sum + $x;
// check the number is even
if( $x % 2 === 0 ) {
// sum only the even numbers
$evensum = $evensum + $x;
}
}
// output
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
Another approach is using range() and array_functions
$arr=range(1,10);
echo $sum=array_sum($arr);
function even($var)
{
return(!($var & 1));
}
echo $even=array_sum(array_filter($arr, "even"));
Sum all ranges
$all_sum=array_sum(range(1,10));
Sum even number (0,2,4,6,8,10)
$even_sum = array_sum(range(0,10,2));

Finding the averege in a loop counting to 100

I need to find the averege after using a loop counting ever 3rd to a 100. The loop part is easy enough, but I need to sum every value then divide the sum on the total of values.
for ($x = 3; $x < 100; $x+=3) {
echo $x.", ";
}
This is the loop I need to use. How to I sum the values this produces and how do I find how many values this loop produces?
I believe the intention here is to learn about loops, otherwise this stuff can be done without looping too.
For learning purpose, you can simply introduce two variables count and sum and compute them inside the loop. For count, you just increment it on each iteration. For sum, you add the current value of x into sum. After the loop you print both variables.
$count = 0;
$sum = 0;
for ($x = 3; $x < 100; $x+=3) {
echo $x.", ";
$count++;
$sum+=$x;
}
echo $sum;
echo $count;
add your elements into an array and then use array_sum to sum the array elements , then divide the sum by the count of your array
$arr = [];
for ($x = 3; $x < 100; $x+=3) {
// echo $x.", \n";
$arr[] = $x;
}
print_r(array_sum($arr) / count($arr));
// Output : 51
$i=0;
$tempx=0;
for ($x = 3; $x < 100; $x+=3) {
//total sum
$tempx = $tempx + $x;
//count of how many times the loop ran in this case 33 times
$i++;
}
//first $i was 0 so we add 1
$i=$i + 1;
//getting the average
$average=$tempx / $i;
echo $average;
//output
For the last answer i think we should not do:
//first $i was 0 so we add 1 $i=$i + 1;
Regards

Getting total value of an array with condition

I need to get the total value of this array of all the numbers above or equal to 0.
This is the array
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
This is the code i have so far, but it only shows the highest number of the array and doesnt count the values up and shows the total.
$totaal = 0;
for($y=0; $y < count($aReeks); $y++)
{
if($totaal < $aReeks[$y] && $aReeks[$y] > 0)
$totaal = $aReeks[$y];
}
I have to do it with a for loop.
Here's a quick way:
$total = array_sum(array_filter($aReeks, function($n) { return $n > 0; }));
Filter the array for values greater than 0
Sum that array
Oh I now see the "I have to do it with a for loop.", so this won't be accepted for your homework I imagine.
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
$total=0;
for($i =0 ; $i< count($aReeks) ; $i++)
{
if($aReeks[$i]>=0)
{
$total+= $aReeks[$i];
}
}
echo $total ;
?>
Output
11859
You are making 2 major mistakes one is in if condition which is $totaal < $aReeks[$y] you don't need this check at all. Secondly rather than summing up the value of each item to the total of all previous items... you are simply assigning the value to the $totaal variable inside the loop.
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
$totaal = 0;
for($y=0; $y < count($aReeks); $y++)
{
if($aReeks[$y] > 0)
$totaal = $totaal + $aReeks[$y];
}

Printing out values according to for-loop conditions

I've created a for loop, but it does not print out the output according to the for loop conditions.
for( $c=0; $c < $total; $c++ )
{
$latestID = AccessControlEntry::orderBy('AccessControlID', 'desc')->first();
$numberID = explode("AC", $latestID->AccessControlID);
$numberID[1] = $numberID[1] + 1;
$newID = "AC";
$newID = $newID.$numberID[1];
}
I grabbed the last ID value (AccessControlID) from the database, and subsequently add 1 ($numberID[1] + 1) for every new entry in AccessControlID.
For eg: $total = 3.
From my understanding of for-loops logic, shouldn't it print out 2 sets of $newID?
Can anyone tell me where/what did I went wrong?
The solution is to bring outside the loop the lines of code which extract out from the DB the value.
$latestID = AccessControlEntry::orderBy('AccessControlID', 'desc')->first();
$numberID = explode("AC", $latestID->AccessControlID);
for( $c=0; $c < $total; $c++ ) {
$numberID[1] = $numberID[1] + 1;
$newID[] = $numberID[1];
}
print_r($newID);

php loop question

hello i'm beginner for programming I've got a homework. googled it but couldnt find anything...
i need to get the total value of numbers from 1 to 10. this need to be done in loop. but couldn't figure which loop should i use. if you can also give me an example code thats would be great.
This is a homework question, I'm not sure why people are just giving you an answer to copy-paste.
Achieving the sum of numbers 1..10 is pretty simple. You will need to initialise an empty int var before your loop, and for each iteration from 0 up to and including 10 you will add your int var to the current iteration.
For example:
sum = 0;
for num in range 1 to 10:
sum = sum + num;
<?php
$start = 0; // set the variable that will hold our total
for($i=1;$i<11;$i++){ // set a loop, read here: http://php.net/manual/en/control-structures.for.php for more info
$start += $i; // add $i to our start value
}
echo $start; // display our final value
I would use a for loop.
$total = 0;
for($i = 1; $i <= 10; $i++){
$total += $i;
}
Using the for loop:
<?php
$sum = 0;
for($i = 1; $i <= 10; $i++){
$sum += $i;
}
Using the foreach loop:
<?php
$sum = 0;
foreach(range(1,10) as $num){
$sum += $num;
}
echo $sum; // prints 55
And disregarding your assignment, here is an easier way:
echo array_sum(range(1,10));

Categories