I have this code:
$a = array ('zero','one','two', 'three');
foreach ($a as &$v) {
}
foreach ($a as $v) {
echo $v.PHP_EOL;
}
Can somebody explain why the output is:
zero one two two .
From zend certification study guide.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because on the second loop,
$vis still a reference to the last array item, so it’s overwritten each time.You can see it like that:
As you can see, the last array item takes the current loop value: ‘zero’, ‘one’, ‘two’, and then it’s just ‘two’… : )