For loop isn't running with php post - php

I'm converting a bash cgi script to php on apache webserver. For whatever reason I have a for loop that doesn't want to execute. I've tried comparing it to other loops I've done in the past and it seems like it should work but nothing shows up on the page in the source code. It's just an empty area. I also put an echo in the code to echo out which number it's on, but those didn't show up either. Anybody see where I'm going wrong?
for($i=$begin; $i>=$end; $i++) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}
I've already verified the variables begin and end show up by echoing them just before the loop. Once I hit the loop it's not executing that code. Everything after the loop executes fine as well. I'm also not getting any errors in the apache log files, which is what really is frustrating. Any help would be appreciated.
Thanks!

I think that you should replace >= to <=

i supposed that
$begin =5;
$end = 0;
then you should used decrements $i-- with >=
for($i=$begin; $i>=$end; $i--) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}
if
$begin = 0;
$end = 5;
then you should less than equal to <= with increment $i++
for($i=$begin; $i<=$end; $i++) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}

I am not sure about the values both the variable have, but you need to change the >= sign to <= so that the loop can go until $i goes up to the $end value.
for($i=$begin; $i<=$end; $i++) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}

Your code is use incremental for $i and in your logic you use >=$end I think you have to change it to $i
for($i=$begin; $i<$end; $i++) { echo "$i\n"; echo "<td>somedata$i</td></tr><tr>\n";

Change either >= to <= or make it a decrement loop by changing i++ to i-- Both will work.

Related

While loop hangs when decrement variable is set in PHP

the while loop hangs with the decrement variable ($j--) set after the echo command..
$j = 10;
while($j>-10)
{
if($j==0)continue;
echo (10/$j) . "<br>";
$j--;
}
but works well when set before if statement
$j = 10;
while($j>-10)
{
$j--;
if($j==0)continue;
echo (10/$j) . "<br>";
}
Can anyone explain pls?
When $j==0 it skips the rest of the code in the loop because of the continue so it never gets to the $j-- in the first code.
This means that $j will never get below 0 and the loop will never finish.
As in the second code it will always decrements it before the test, it will eventually get to -10.
Try adding
echo $j.PHP_EOL;
as the first line in the loop (assuming you are using the CLI version) to see what is happening.
Another slightly different version of your code can be used to ONLY avoid the echo when $j is 0, instead of doing the continue...
$j = 10;
while($j>-10)
{
if($j != 0) {
echo (10/$j) . "<br>";
}
$j--;
}

Can I assign same variable for multiple loop

Case 1 loop inside another loop Can I assign both the $i variable for incrementing?
for($i=0; $i < 10; $i++)
{
for($i=0; $i < 5; $i++)
{
echo "You are too cute";
}
}
Case 2 : if it's not inside, Could I declare $i for both?
like this
for($i=0; $i < 10; $i++)
{
}
for($i=0; $i < 5; $i++)
{
}
There are already some answers that are just fine, but here's a slightly different perspective.
It depends on what you mean by "can". You can do this in the sense that it is syntactically correct PHP code.
for($i=0; $i < 10; $i++)
{
for($i=0; $i < 5; $i++)
{
echo "You will never see this text in your browser.";
}
}
But because a loop does not introduce a new variable scope in PHP, it creates an infinite loop.
The outer loop will execute once, then the inner loop will reset $i to 0, increment it to 5, return control to the outer loop, which will execute once, immediately causing the inner loop to start again, resetting $i to 0 and incrementing it to 5, and so on, forever (at least until your script times out). The outer loop can never end because the inner loop affects $i so that it can never satisfy the exit condition of the outer loop.
In other words, just use a different variable for the inner loop. Removing one integer variable is not going to be a noticeable optimization of your code, if that's what you're concerned about.
In the second example, there's no reason not to reuse $i.
Case 1: It will you get a really odd result, check it out here
How to do it properly? Check that out here
for($i=0; $i < 10; $i++){
for($k=0; $k < 5; $k++){
echo "1)".$i." 2)".$k."\n";
}
echo "\n";
}
Case 2: Works fine as stated in above comments and other answer. But, may I also add that in for instance this example.
for($i=0; $i < 10; $i++){
echo $i."\n";
}
echo "\n Outside the loop: ".$i." \n";
for($i=0; $i < 5; $i++){
echo $i."\n";
}
You can acces $i still after the loop has happend. The reason why you can use $i again is because you are declaring it $i =0; again, without interest toward another loop that is currently running (as is happening in case 1).
You can test this example here
Case 1 : No, you cant. you need to create variable individually for each loop.
for($i=0; $i < 10; $i++)
{
for($j=0; $j < 5; $j++)
{
echo "You are too cute";
}
}
Case 2 : Yes you can.
for($i=0; $i < 10; $i++)
{
echo "You are too cute";
}
for($i=0; $i < 5; $i++)
{
echo "You are too cute";
}
Case 1:
Short answer No you can't .
Long Answer .
First you need to understand what actually a variable is and How that Loop actually works .
Each and every variably is actually a reference to memory. In you example you have created a variable named $i and it can't be greater or equal 10 after incrementing value by one.
In the machine level it is translated to an address in the memory. say for example $i points to a random address 0xF25 When ever you loop it and incrementing it, the next address becomes 0xF30.
When ever you write a for loop, compiler automatically assigns a fixed memory address and that address it limited to your variable scope.
What compiler does is, it creates a table for that token($i). In simple form Look below an example
$i(This is the token ) -> 0xF25 (This is the value)
This value is updated when you do $i++
In nested Loop compiler assigns same table(though outer loop cant access inner loop variables). If compiler puts same variable for inner loop, it will be contradictory. Because inner loop may start from memory address 0xE21. In that case when your outer loop increment value by One it will be 0xE22 but as discussed above it needs to be 0xF30 .
That is why compiler does not allow this and we need to use CASE 2 example.

how to get a variable value that will be changed later

i have this code
$number = 1;
echo $number;
for ($i=0; $i < 10; $i++) {
$number++;
}
the output of echo $number is 1 not 11.
How can get the last $number value when I called it before it changed?
Once you output something to the browser, it's done. You cannot change it again later. The only way to handle this is to not output the variable until you have found its final value; ie in the example you move the echo statement to the bottom.
It's generally considered a good idea to first run all of your PHP code and determine all your variables and only then start outputting things to the browser in order to prevent the kind of problem you have now.
$number = 1;
echo 'before increment :'.$number;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo 'after increment :'.$number;
Try this way, now you will get expected result:
$number = 1;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo $number;
Reason, you have put echo $number before the increment, which was logically wrong:

ncurses not deleting character

Alright, I'm working on a (supposed-to-be) simple counting script using ncurses. Everytime it increments the number, I need it to delete the previous number before adding another number, so that it updates rather than appends.
Here is my code:
<?php
ncurses_init();
$i = 0;
$nStr = "Number: ";
ncurses_addstr($nStr);
ncurses_refresh();
for ($i=0; $i < 100; $i++)
{
$iLen = strlen($i);
for ($j=0; $j < $iLen; $j++)
{
ncurses_delch();
}
ncurses_addstr($i);
ncurses_refresh();
sleep(2);
}
ncurses_end();
?>
Currently when I run it, it outputs like this: Number: 01234[...]
Anyone see where my problem is and how I can fix it?
ncurses_delch() forward-deletes. If you want to move the cusor back one column then output \b instead.

How do you remember the order of the expressions in a for loop?

It's a pretty simple question, I always have to go check here and then I hit my head and say it's so obvious. But really after a week of not using it I usually end up writing
for ($i = 1; $i++; $i <= 10;) {
echo $i;
}
some Mnemonic might help
ICE:
Initialisation
Check
Execute
Think logical! The order is the same as the expressions are evaluated.
for ($i = 0; $i < 10; ++$i) {
echo $i;
}
// is same as
$i = 0; // 1.
while ($i < 10) { //2.
echo $i;
++$i; // 3.
}
They go in order.
for (expr1; expr2; expr3)
expr1: Evaluated once at the beginning of the loop
expr2: Evaluated at the beginning of each iteration of the loop
expr3: Evaluated at the end of each iteration of the loop
You want to initialize first, check the condition second, and increment (or decrement) your counter last.
START -> CHECK FOR DANGER -> MOVE AHEAD
for( $i = 0 ; $i < 100 ; $i++ )
Hope it helps :-)
Best of luck!
F
irst (initialisation)
O
Only while (condition)
R
Rolling on (incrementing or decrementing)
I may be daft but don't you want this structure:
for ( $i = 1; $i <= 10; $i++ )
{
echo $i;
}
I don't know of a Mnemonic to remember this structure I've always just seen it as:
STARTING OFF; DO WHILE THIS; PERFORM AFTER EACH ROTATION
Rather:
DEFINE PRIOR TO EXECUTION; DEFINE EXECUTION LIMITS; DEFINE OPERATION FOR EACH ROTATION
Just remember that the guard is always checked before the increment, so you write it before.
If you don't remember the guard is checked before the increment, you're in bigger trouble, because you don't know what the loop will do :p
SAM
Start your engine
Are we there yet?
Move

Categories