note 21328 deleted from control-structures.foreach by sniper

From: Date: Wed, 08 Oct 2003 04:09:11 +0000
Subject: note 21328 deleted from control-structures.foreach by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58010@lists.php.net to get a copy of this message
Note Submitter: juan@rupture.net ---- 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 ).

« previous php.notes (#58010) next »