php beginner,我可以为多个循环分配相同的变量

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 : 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: 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."
";
  }
  echo "
";
}


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."
";
}

echo "
 Outside the loop: ".$i." 
";

for($i=0; $i < 5; $i++){
   echo $i."
";
}

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: 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.