note 76133 added to control-structures.foreach

From: Date: Mon, 02 Jul 2007 08:08:36 +0000
Subject: note 76133 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-128151@lists.php.net to get a copy of this message
Alright, I had a little error. I had one foreach() declaration, and then another foreach() declaration. They went: <?php //$connections is an array of Socket resources foreach ($connections as $key => &$value) { //the code here is impertinent } //$users is an associative array foreach ($users as $key => &$value) { //the code here is impertinent } ?> Alright, now, what error was produced as a result of this? This one: "Warning: Cannot use scalar value as array in filename.php on line 69." I then realized something; the reason for this came from the fact that I used $key, and $value for both of them in the exact same way. As a response to this, I've developed two ways to fix this: <?php //add this to the end of every foreach() you use unset($key,$value) ?> OR Simply use different variables for each one. ---- Server IP: 208.69.120.35 Probable Submitter: 69.86.30.92 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/76133 Del: integrated -- https://master.php.net/note/delete/76133/integrated Del: useless -- https://master.php.net/note/delete/76133/useless Del: bad code -- https://master.php.net/note/delete/76133/bad+code Del: spam -- https://master.php.net/note/delete/76133/spam Del: non-english -- https://master.php.net/note/delete/76133/non-english Del: in docs -- https://master.php.net/note/delete/76133/in+docs Del: other reasons-- https://master.php.net/note/delete/76133 Reject -- https://master.php.net/note/reject/76133 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128151) next »