I try to get the summtion of the values inside a for loop but i couldn't do that
like this code:
for($i=1;$i<=($value);$i++){
if(isset($_POST['submit'])){
$total=$max[1]+$max[2]+......+$max[$i];
echo $total;
}
}
When i press submit it gave me that there's a mistake in line 2
that there is an unexpected '.' and i couldn't find any solution to solve this problem
i hope that my problem is clear for you
can any one help me please :)
if ( isset($_POST['submit']) ){
$total = 0;
for( $i = 1; $i <= $value; $i++ ){
$total += $max[$i];
}
echo $total;
}
if(isset($_POST['submit'])){
$total = 0;
for($i=1; $i <= $value; $i++){
$total += $max[$i];
}
echo $total;
}
Related
Can anyone help me understand why a variable takes its initial value after incrementing the variable? below is code:
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
echo $k+3;
echo $l+3;
}
In this we have two for loops running one inside other. Here we run three times the outside for loop, inside this we are running other for loop again. The problem we are facing is that when inner for loop end we have incremented $k and $l both by 3 but it always take value 0 and 3 respectively.
we have incremented $k and $l both by 3
Nope, you only print the result of your values plus 3, but you do not set them anywhere in the loop:
Instead of
echo $k+3;
echo $l+3;
write
$k = $k + 3;
$l = $l + 3;
You should try removing the "echo" and incrementing the variable in each loop. Then print them out after.
Try this:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
$j = $j++;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
?>
Gives you:
9
12
TRY this.
$k += 3;
$l += 3;
echo $k;
echo $l;
Try This
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
First Increment the value and store it in the variable
$k = $k+3;
$l = $l+3;
Then You need to print using
echo $k.'<br>';
echo $l;
#Harinarayan First of all you need to read about echo() http://php.net/manual/en/function.echo.php
echo — Output one or more strings
echo does not manipulate expression as you did in your question like:
echo $k+3;
instead of using echo for the increment you should first increment the variable and then echo that variable like below:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k += 3;
$l += 3;
echo $k;
echo "<br>";
echo $l;
}
for ($i = $field +1; $i < $field2; $i++) {
echo $i.'<br/>';
}
what i'm trying to do here is Echo numbers between the input values field and field2, this works, however i was wondering if there was a more efficient way of doing this (eg changing the $i = $field +1;)
Another using while loop example:
<?php
$i = $field;
while(++$i < $field2) {
echo $i.'<br/>';
}
I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
I know i Can easily do this with a for loop but i am trying to do it in while loop and I get Google infinite times. Why is this? I Knew something was wrong however because i was going over a tutorial on tuts+ I thought it was my mistake. Then I read the comments section of the video and the instructor says sorry I forgot to increment the I.
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
while ( $i < 10) {
echo "<li>$month[$i]</li>";
}
?>
Add i++ after the echo statement.
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
while ( $i < 10) {
echo "<li>$month[$i]</li>";
$i++;
}
It happens because $i is always equal to 0 in your code.
You need to increment it (as stated in the other solution) for instance using $i++ in the loop.
Note that it's generally better to use foreach which will iterate over each element of the array:
$months = array('google', 'html5nurse' , 'facebook');
foreach($months as $month){
echo $month."<br/>";
}
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
while ( $i++ < 10)
{
echo "<li>$month[$i]</li>";
}
?>
You should be using $i++ to increment $i and also using count so that the loop does not continue after there are no more results in the array.
<?php
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
$count = count($month);
while ($i < $count)
{
echo "<li>$month[$i]</li>";
$i++;
}
?>
I've been learning PHP for a few days and I have an embarrassingly easy question. I want to sum the items in a 500 item loop. I want to figure out the total of 500 + 499 + 498, etc.
Here's my code:
for ($i=1; $i<=500; $i++)
{
// echo $i . "<br />";
$total = 0;
$total = $total + $i;
return $total;
}
echo $total . "<br />";
?>
Can't figure out what I'm doing wrong.
Pull out the initialization and the return statement from the loop:
$total = 0;
for ($i=1; $i<=500; $i++) {
echo $i . "";
$total = $total + $i;
}
echo $total . "";
return $total;
You can also do
echo array_sum(range(0,500)); // 125250
or do the entire calculation without a for body:
for( $total = $i = 0; $i <= 500; $total += $i++ );
echo $total; // 125250
and a couple of other approaches (Daniel's solution is particularly nice).
Both of the above are equivalent to what you are likely looking for
$total = 0;
for ($i=1; $i<=500; $i++) {
$total = $total + $i;
}
echo $total;
Like already pointed out elsewhere, when you do $total = 0; inside the for loop, you will overwrite the previous value for $total and putting return into it will end your script unless the loop is inside a function.
It looks like you simply need to move the $total = 0; line out of your loop... Otherwise it will be set to 0 on each iteration.
You also need to move that return $total; line outside, as #Webnet noted in a comment below.
Also note that you can do that calculation in constant time without iterating through all the numbers, by finding the sum of an arithmetic progression:
Sn = 1/2n(a1 + a2)
Sn = 250(1 + 500)
= 125250
Your code should be...
$total = 0;
for ($i=1; $i<=500; $i++) {
// echo $i . "<br />";
$total = $total + $i;
}
echo $total . "<br />";
return $total; will break the loop
Well, you're resetting $total to zero every time, and using return outside of a statement...
Anyways, the answer is (500*501)/2 = 250*501 = 125250 (basic maths)
You have two errors:
First, you set the $total to 0 each time the loop is repeated.
Then, you use return, what happens here is that it will cancel further execution, so the loop basically just was run once.
Try this.
$total = 0;
for ($i=1; $i<=500; $i++){
$total = $total + $i;
}
echo $total;
$total = 0;
for ($i=1; $i<=500; $i++) {
echo $i . "";
$total = $total + $i;
}
echo $total . "";
return $total;