<?php
$customer = ["aa_","aa_","aa_","aa_"];
$i = 0;
do{
$customer[$i] = $customer[$i].$i;
$i++;
} while ($i === 4);
echo $i; //print only 1
var_dump($customer);
?>
$customer
实际输出
array (size=4)
0 => string 'aa_0' (length=4)
1 => string 'aa_' (length=4)
2 => string 'aa_' (length=4)
3 => string 'aa_' (length=4)
当我使用while ($i < 4)
正在工作时。但在这里我误解了为什么使用about condition只能循环一次......我认为它应该是
do $i++ -> 1 ( 1 === 4 ) loop again
do $i++ -> 2 ( 2 === 4 ) loop again
do $i++ -> 3 ( 3 === 4 ) loop again
do $i++ -> 4 ( 4 === 4 ) stop quit
所以预期$customer
输出
array (size=4)
0 => string 'aa_0' (length=4)
1 => string 'aa_1' (length=4)
2 => string 'aa_2' (length=4)
3 => string 'aa_3' (length=4)
答案 0 :(得分:1)
不,你的解释是错误的。
$i = 0 // Starting value
do (Will do the action here) $i++ -> 1
CONDITION ( 1 === 4 ) -> FALSE so it will not loop, (Stop the loop) so $i is only = 1;
如果你真的想在你的条件下使用equals,你应该使用!==
(不等于),如下所示:
$i = 0;
do{
$customer[$i] = $customer[$i].$i;
$i++;
} while ($i !== 4);
所以解释是这个
$i = 0 // Starting value
do (Will do the action here) $i++ -> 1
CONDITION ( 1 !== 4 ) -> TRUE (Loop continues)
do (Will do the action here) $i++ -> 2
CONDITION ( 2 !== 4 ) -> TRUE (Loop continues)
do (Will do the action here) $i++ -> 3
CONDITION ( 3 !== 4 ) -> TRUE (Loop continues)
do (Will do the action here) $i++ -> 4
CONDITION ( 4 !== 4 ) -> FALSE (Stops the loop)
Final value of $i will be 4.