Having Issue On PHP Increment [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Can you please take a look at this demo and let me know why I am not able to increment the $start as the output is looks like 88888 only
<?php
for($i = 1; $i<= 5; $i++){
$start = 8;
echo $start;
$start++;
}
?>

That is because you always assign your value inside loop , take it out
<?php
$start = 8;
for($i = 1; $i<= 5; $i++){
echo $start;
$start++;
}
?>

If you want to increment the value of start, you must declare it outside of the for loop.
<?php
$start = 8;
for($i = 1; $i<= 5; $i++){
echo $start;
$start++;
}
?>

Related

PHP: loop through an array give wrong result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have following code:
$source_stat_array = array(0 => 'clicks', 1 => 'impr', 2 => 'spend');
$count = count($source_stat_array);
for ($i = 0; $i < count; $i++) {
echo $source_stat_array[$i++];
}
the result i get is following:
clicks spend
instead of i need
clicks impr spend
can you answer me what is wrong with code?
You are incrementing the value of $i twice.
Try the below code:
for ($i = 0; $i < $count; $i++) {
echo $source_stat_array[$i];
}
You can use foreach it will iterate through each key
foreach($source_stat_array as $key => $value){
echo $value;
}
Read more about foreach , manual

for loop doesn't after looping throw the script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have php script to do for loop, it supposed to loop over the code once and then exit,but theses not happening, it keep looping till i exited manually.
my code
<?php
session_start();
?>
<?php
ignore_user_abort(true);
for( $x=0; $x< 50; )
{
$_SESSION['timeout'] = time();
$st = $_SESSION['timeout'] + 1;
session_unset();
$_SESSION['timeout'] = time();
$st = $_SESSION['timeout'] + 1;
print_r("$st\n");
}
if(time() < $st)
{
file_put_contents('/tmp/phptest1234.txt', 'test');
}
?>
Below is an infite loop. $x always stays at 0.
for( $x=0; $x< 50; )
You need to do:
for( $x=0; $x< 50; $x++)
php man "for"

How can I jump out of a loop but carry on at the next iteration? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In PHP, break exits a loop at a given point. But is it possible to force the loop to jump to the next iteration at a given point, rather than exiting it completely? Essentially:
for ($i = 0; $i < $foo; $i++){
if ($i == 1){
gotoNextIteration;
} else {
//do something else
}
}
Use continue for this purpose
for ($i = 0; $i < $foo; $i++){
if ($i == 1){
continue;
} else {
//do something else
}
}
Either use continue; or you can do something like this as well
for ($i = 0; $i < $foo; $i++){
if ($i != 1){
//do something
}
}
you don't need anything else.
Yes .. i have built many type of these loops for dynamic content:
for ($i = 1; $i <= 20; $i++) {
if ($i == 1) {
// write table header
} else if ($i == 20) {
// write the table footer
} else {
// fill the table columns
}
}
This is a basic example, but I use it for data iteration (like image galleries) and dynamic tables, and more. but I bow to Abhik Chakraborty's Answer as i was not familiar with "continue". I love learning new stuff.

How to increment a for loop by more than 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I print a series like this?
6,15,24,33,42.....1000
I have tried using a for loop like this, but can't work out how to get it to increment by 9 on each iteration.
for($i = 6; $i <= 1000; $i = 9)
{
echo $i . ', ';
}
Quite simple really:-
for($i = 6; $i <= 1000; $i += 9){
echo $i . ', ';
}
//If you have to finish the series with 1000, although it is not a part of the series:-
echo '1000';
See it working
just for fun:
echo implode(', ', range(6, 1000, 9));
echo ", 1000";
<?php
for($i=6; $i<1000; $i+=9)
echo $i."<br>";
?>
Seeing the question this can be an answer.

Implement two-dimensional array [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I need to implement two-dimensional array in PHP. Is it a correct way to do this?
$constr = array();
for ($i = 0; $i < $size; $i++)
{
for ($j=0; $j < $ncons; $j++) {
$constr[$i][$j] = $set->getInd($i)->getConstr($j);
}
}
The code as you have it is fine, but since you're using objects, it's best to cache them inside the outer loop:
$constr = array();
for ($i = 0; $i < $size; $i++) {
$ind = $set->getInd($i);
for ($j=0; $j < $ncons; $j++) {
$constr[$i][$j] = $ind->getConstr($j);
}
}
In this way, you're not repeating $set->getInd($i) for the inner loop.

Categories