RE: [PHP] Odd behavior with unset, array indexes, and types

From: Date: Thu, 17 Jun 2004 22:16:36 +0000
Subject: RE: [PHP] Odd behavior with unset, array indexes, and types
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-188514@lists.php.net to get a copy of this message
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 >> it seems that unset() will not remove an array element when you >> refer to its key using a double, although isset() will return true >> when referenced the same way. If I cast $b to either an int or a >> string, the unset call works. Am I just missing the portion of the >> manual that documents this behavior, or is this a bug? Just thought >> I'd see if anyone had run across this before...TIA >> > == does not check type in php. Yes, I'm aware of that. That's not what my post was about. > 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"; } if (isset($a[(double) 2])) { print "isset() doesn't have a problem casting the array index.\n"; } unset($a[(double) 2]); if (isset($a[2])) { print "It appears that only unset() has a problem with this.\n"; } </quote> generates: <quote> Arrays indexes are not type specific, at least not in all cases. Integers and doubles compare, even when using '=='. isset() doesn't have a problem casting the array index. It appears that only unset() has a problem with this. </quote> It's the behavior that is specific to unset() that I'm puzzled about.

« previous php.general (#188514) next »