note 112590 added to control-structures.foreach

From: Date: Tue, 02 Jul 2013 13:43:47 +0000
Subject: note 112590 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-195433@lists.php.net to get a copy of this message
I want to add some inline comments to dtowell's piece of code about the iteration by reference: <?php $a = array('abe','ben','cam'); foreach ($a as $k=>&$n) $n = strtoupper($n); # At the end of this cycle the variable $n refers to the same memory as $a[2] # So when the second "foreach" assigns a value to $n : foreach ($a as $k=>$n) // notice NO reference here! echo "$n\n"; # it is also modifying $a[2] . # So on the three repetitions of the second "foreach" the array will look like: # 1. ('abe','ben','abe') - assigned the value of the first element to the last element # 2. ('abe','ben','ben') - assigned the value of the second element to the last element # 3. ('abe','ben','ben') - assigned the value of the third element to itself print_r($a); ?> ---- Server IP: 69.147.83.199 Probable Submitter: 62.176.102.230 ---- Manual Page -- http://php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/112590 Del: integrated -- https://master.php.net/note/delete/112590/integrated Del: useless -- https://master.php.net/note/delete/112590/useless Del: bad code -- https://master.php.net/note/delete/112590/bad+code Del: spam -- https://master.php.net/note/delete/112590/spam Del: non-english -- https://master.php.net/note/delete/112590/non-english Del: in docs -- https://master.php.net/note/delete/112590/in+docs Del: other reasons-- https://master.php.net/note/delete/112590 Reject -- https://master.php.net/note/reject/112590 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195433) next »