Zend Certification practising

I recently decided it was time to finally go for the Zend Certification Exam. Armed with the Zend 5 Certification Bundle (being the Study guide book, 10 test exams and a voucher to take the actual exam), I’m now ready to study and hopefully pass the certification.

One of the things I’m glad for are the small bits of code and theory in the study guide that still surprise me and that I’m still able to learn and improve myself on the knowledge of PHP and how it works internally.

Take, for example, the following code:

$a = array('zero', 'one', 'two');
foreach($a as &$v) {}
foreach($a as $v) {}

You would expect for it to output array('zero', 'one', 'two'), but it does in fact output array('zero', 'one', 'one').

How is this possible? What happens in this case is after line 2, $v is a reference to $a[2]. On line 3, the foreach assigns a value to $v on each loop, which is still a reference to $a[2], so 3 loops take place:

$v ($a[2]) = $a[0], namely ‘zero’
$v ($a[2]) = $a[1], namely ‘one’
$v ($a[2]) = $a[2], itself!

As the last assignment tries to set it’s own value, it just uses the last value that was set to it, being ‘one’.

If the following concise explanation isn’t very clear to you, then you have a good reason to get the Certification guide and start studying! :)

To be continued, I’ll let you know when I’ll hopefully pass the exam :)