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>';
}
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"
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 8 years ago.
Improve this question
My code:
$rank_content = file('https://www.championsofregnum.com/index.php?l=1&ref=gmg&sec=42&world=2');
$line_count = 0;
//initializing only the first few keys because of no reason (the latter ones aren't in use yet)
$rankNameArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
$rankRlmpArr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72);
while ($line = array_shift($rank_content)) { // retrieving line after line of the website, does work indeed
$line_count += 1;
if(strpos($line, "Warrior #") || strpos($line, "Archer #") || strpos($line, "Mage #"))
{
$rankNameArr[$line_count] = $line_count + 1; // HERE nothing happens
$rankRlmpArr[$line_count] = $line_count + 2; // nothing happens here, too
}
}
Why does
echo $rankNameArr[2];
echo $rankRlmpArr[2];
give me the value 42 instead of the correct value? If I replace $line_count with a real number, the script works properly.
My intention is to store the value $line_count + 1 into $rankNameArr at position $line_count. Not very complicated actually
EDIT -------------
Forget everything above, please. I finally reduced the script to the actual problem:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$arr[$counter]=$i;
}
echo $arr[5];
This sadly echoes 42. I have no idea how to have $arr[$counter] store the actual value of $i.
I have no idea how to have $arr[$counter] store the actual value of $i.
$counter is set to 0, so $arr[$counter] is the same as saying $arr[0]. If you echo $arr[0] you'll see that it is being changed while the rest are left alone, which means your code is working.
However if you want $counter to increment also, you just need to tell it to do so:
$arr = array(0=>42,1=>42,2=>42,3=>42,4=>42,5=>42,6=>42);
$counter=0;
for($i=0;$i<7;$i++) {
$counter++;
$arr[$counter]=$i;
}
echo $arr[5];
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.