note 21328 added to control-structures.foreach

From: Date: Wed, 08 May 2002 18:37:14 +0000
Subject: note 21328 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-30339@lists.php.net to get a copy of this message
Interesting behaviour that might save some people time and head scratching: //a demonstration of how a key reverts from //string to integer, even if explicitly defined, if its //corresponding array is subject to a foreach loop $key = 1; settype( $key,"string" ); echo( "First:" ); echo( gettype( $key ) ); echo( "<br>" ); $arr[$key] = "testing"; foreach( $arr as $key=>$val ) { echo( "Second:" ); echo( gettype( $key ) ); echo( "<br>" ); } Its a potential problem if you're walking through the array and comparing the key to another variable and doing so exactly. i.e. ( $foo === $bar ). -- http://www.php.net/manual/en/control-structures.foreach.php http://master.php.net/manage/user-notes.php?action=edit+21328 http://master.php.net/manage/user-notes.php?action=delete+21328 http://master.php.net/manage/user-notes.php?action=reject+21328

« previous php.notes (#30339) next »