I found such php code :
$a = 5;
$i = 4;
for($i = &$a; $i < 10; ++$i);
echo "a=$a, i=$i";
I would expect this loop to be executed 4 times since $i becomes a reference to a$(right?). However loop is executed only once and outputs :
a=10, i=10
I can't figure outh why it works like this. Any ideas?
It works because the for statement is being processed like a single line statement as a result of the semicolon.
for($i = &$a; $i < 10; ++$i);
can also be seen as
for($i = &$a; $i < 10; ++$i) echo $i;
Without the curly brackets, the following line will be considered part of the for statement, your line being just a semicolon to indicate the end of the line.
The proper code would look like this.
$a = 5;
for($i = $a; $i < 10; ++$i) echo "a=$a, i=$i";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.