for loop doesn't after looping throw the script [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 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"

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

PHP for loop increment by 100000 [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 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>';
}

Having Issue On PHP Increment [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 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++;
}
?>

How to add to a conditional variable for comparison [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
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';
}

Php operator `break` doesn't stop while [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
Have a weird issue, break doesn't break while loop and keep rotating. Logically it should break the cycle and I set some output
while ($c++ != 5)
{
// here is curl request, doesn't relevant so I have removed it
preg_match("!<.*?>(\S*)[^<>]*?{$to}<!", $resp, $result) ;
//
echo 'cnt = ' .count($result).'<br>';
if(!count($result))
{
$amount = 0;
echo 'sleep called';
sleep(4);
exit;
continue;
}
else
{
$amount = trim(#$result[1]);
$amount = round($amount + $amount * $exchange_ratio, $precision);
echo 'break happen<br>';
break;
}
}
so output is
cnt = 2
break happen
cnt = 0
sleep called
So it skipped break and keep rotating. How so?
EDIT
works fine on my localhost so issue probably with php version?
You must be running this code multiple times: is there a second call to this script, or a second loop around this part? Look at my test code: it clearly breaks after the break:
$ cat tmp.php
<?php
$c = 0;
while ($c++ != 5)
{
if(false) //just emulating you
{
$amount = 0;
echo 'sleep called';
sleep(4);
exit;
continue; //this will never be called by the way
}
else
{
echo 'break happen<br>';
break; //this indeed breaks out of the while
}
}
$ php tmp.php
break happen<br>$
(the $ is the next prompt, on the same line as I did not put in a newline :) )

Categories