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 5 years ago.
Improve this question
I want to loop through $i which starts from 790000000 and echo every 100000 up to 799999999:
790000000
790100000
790200000
790300000
790400000
...
I tried this code but it didn't work:
for ($i=790000000; $i<=800000000; $i+100000) {
echo $i . '<br>';
}
For loop wrong code, you are missing actual code update as $i+100000 does not update variable. Use $i += 100000 instead.
// Here is problem in your code
for ($i=790000000; $i<=800000000; $i += 100000) {
echo $i . '<br>';
}
Update your loop using += instead of i.
Only using + will not increment your $i variable value.
for ($i=790000000; $i<=800000000; $i+=100000) {
echo $i . '<br>';
}
Related
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
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++;
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
How do you sort this empty array using foreach loop to output these 25 numbers in order?
Example outcome would be: 1 1 1 1 1 2 2 2 2 3 3 3 3 3 4 4 4 4
<?php
$array = array();
for($counter = 0; $counter <=25; $counter++){
$die = rand(1, 10);
$int[$counter] = $die;
echo " $die ";
}
sort($die);
foreach ($die as $value)
{
echo '<option value="'.$value.'">'.$value.'</option>';
}
?>
<?php
$array = array();
for ($i = 0; $i <=25; $i++) {
echo $array[] = rand(1, 10);
}
sort($array);
foreach ($array as $value) {
echo '<option value="'.$value.'">'.$value.'</option>';
}
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
At this point in time, the below variables are set at the values shown and I need to compare:
(Pseudo code)
if $foo is > 0 AND that $i == $bar +1.
This is just a simplified example. In my code these variables are different at various times.
I'm not having any success figuring out how to do this comparison. Any help is appreciated.
$i = 4;
$foo = 4;
$bar = 3;
if($foo > 0 && ($i == ($bar + 1))) {
echo 'do something';
}
Thanks in advance for any help pointing me in the right direction.
Honestly think your code should work (after the parentheses fixes), but whatever your real logic is instead of the example code you gave, you can try wrapping all the logic areas.
$i = 4;
$foo = 4;
$bar = 3;
if(($foo > 0) && ($i == ($bar + 1))) {
echo 'do something';
}
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"