While循环PHP

why this code is output of 8?

<?php
    $i = 1;
    while ($i <= 7) {
        $i++;
    }
  echo "The value is $i <br />";
?>

but when you put this code on open and close parenthesis
echo $i;

the output is

12345678

Please Explain how.. Thanks!

I'm struggling to understand what you mean by:-

but when you put this on open and close parenthesis "echo "

With that said however I can certainly help explain why the output is 8.

Your counter starts at 1 ($i = 1). You then run a while loop that checks if the value of your counter is less than or equal to 7. You're incrementing the counter by 1 on each iteration of the loop.

When $i equals 7 the loop runs again (<=); $i is incremented to 8.

Basically in your first case the echo is executed after the loop and only once. There the value is 8, hence the result you observe.

By putting the echo in the loop, it is executed for each iteration.

You made a while function in which you add a condition to run function until the $i is less than or equal to 7 ( while ($i <= 7) ). So when $i ==7 the fuction run again then the $i ==8 now your condition is wrong so he break the loop and output you 8.

7<7 is false but 7=7 is true, so your loop executes once again and the output is 8. Put echo in the while loop if you want to print each and every output of loop.

while loop will execute when given condition is true. So here 

- $i = 1; is less than or equal 7 so again loop. This time $i increased +1 now, $i = 2 again condition is true, now, $i = 3 again condition is true, now, $i = 4 again condition is true, now, $i = 5 again condition is true, - now, $i = 6 again condition is true, now, $i = 7 again condition is true, now, $i = 8 Here condition is False so loop is ended, So next line will be executed here $i value is 8. so you getting output is 8.