Re: Odd behavior with unset, array indexes, and types

From: Date: Fri, 18 Jun 2004 05:46:01 +0000
Subject: Re: Odd behavior with unset, array indexes, and types
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-188530@lists.php.net to get a copy of this message
* Thus wrote Michael Sims: > Thomas Goyne wrote: > > On Thu, 17 Jun 2004 16:52:32 -0500, Michael Sims > > <michaels@crye-leike.com> wrote: > >> ceil() returns a variable of type double. In the above script I > >> expected $foo to become an empty array after calling unset(). But > > > However, > > array indexes are type specific, so $a[2] != $a['2']. > > That's incorrect (at least with PHP 4.3.7). PHP will happily cast your array index > and compare it without regard to type in some cases, as a simple test illustrates: > > <quote> > $a[2] = 1; > if ($a[2] == $a['2']) { > print "Arrays indexes are not type specific, at least not in all cases.\n"; > } > > if ($a[2] == $a[(double) 2]) { > print "Integers and doubles compare, even when using '=='.\n"; > } > To simplify things: $a[2] = '1'; $k = (double)2; echo isset($a[$k]); unset($a[$k]); echo isset($a[$k]); echo " -> expect 1\n"; Result: 11 -> expect 1 > > It's the behavior that is specific to unset() that I'm puzzled about. > The problem is, array hash's and indexes must be either integer or string as noted in the manual, so technically it really isn't a bug even though it appears to behave like one. I would have to agree that there does seem to be inconsistencies between isset and unset, So perhaps mabey submiting a Feature Request showing how close php seems to supporting (floats) as indexes. You're alternative would be to cast your double to a int or string: $k = (int)(double)2; or to make the above not look too silly: $k = (int) ceil(3/4); HTH, Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!

« previous php.general (#188530) next »