Re: [PHP] Odd behaviour of non-existent keys?
Hi folks, Michael Sims wrote: MS IMHO what you have described is a bug in PHP, and if I were you, MS I'd report it as such. If it's not a bug it at least has a very MS high WTF factor. Problem with reporting is that I am using Debian Test and the current PHP version is too old to report. If someone with an current PHP version could replicate the problem, let me know and I will report it. Here is the code: $foo = '' ; $foo['one']['two'] = 'test-value' ; print( 'bRunning tests for one non-existent key/bbrbr' ) ; // Test isset() for non-existent key if( isset( $foo['one']['two']['three'] ) ) { print( Unexpected isset(): evaluated to TRUEbr ) ; } else { print( Expected isset(): evaluated to FALSEbr ) ; } // Use var_dump() to find out what is going on // Expected: evaluate to NULL // Actual: evaluated to 'string(1) t' // (x is the first char of test-value) print( 'var_dump() output (expecting NULL):br' ) ; var_dump( $foo['one']['two']['three'] ) ; // Add another non-existent key and things // work as expected print( 'brbrbRunning tests for two non-existent keys/bbrbr' ) ; if( isset( $foo['one']['two']['three']['four'] ) ) { print( Unexpected isset(): evaluated to TRUEbr ) ; } else { print( Expected isset(): evaluated to FALSEbr ) ; } print( 'var_dump() output (expecting NULL):br' ) ; var_dump( $foo['one']['two']['three']['four'] ) ; -- Geoff Caplan Vario Software Ltd (+44) 121-515 1154 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Odd behaviour of non-existent keys?
Geoff Caplan wrote: Michael Sims wrote: IMHO what you have described is a bug in PHP, and if I were you, I'd report it as such. If it's not a bug it at least has a very high WTF factor. Problem with reporting is that I am using Debian Test and the current PHP version is too old to report. If someone with an current PHP version could replicate the problem, let me know and I will report it. Here is the code: [snip] I've replicated it in version 4.3.8, as well as five other previous versions (I always keep my build directories around for a while on my development server.) Here's a suggestion so that you don't have to depend on other people to reproduce it for you (and in case one of the PHP maintainers asks you to try a snapshot): Download the PHP tarball you want to test with (4.3.8 or snapshot or whatever) and extract it in your home directory, then configure it and run make. Then create your test script as a CLI script and make the shebang line: #!/home/your-home-dir/php-version/sapi/cli/php This allows you to test with whatever version you wish without having to actually install it. FWIW... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd behaviour of non-existent keys?
Thanks for your help, everyone, especially Michael Sims. I've reported the bug: http://bugs.php.net/bug.php?id=29848 -- Geoff Caplan Vario Software Ltd (+44) 121-515 1154 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Odd behaviour of non-existent keys?
Hi folks, Getting a result I don't understand. Why does the non-existent key in the array below evaluate to the first char of the array value? $foo = '' ; $foo['one']['two'] = 'test-value' ; // Evaluates to: string(1) t var_dump( $foo['one']['two']['three'] ) ; // Evaluates to NULL, as expected var_dump($foo['one']['two']['three']['four'] ) ; Can anyone enlighten me? I spotted this because I was getting unexpected results with isset() used on multi-dimensional arrays. I'm using the latest version of PHP4 on Debian. -- Geoff Caplan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Odd behaviour of non-existent keys?
From: Geoff Caplan [EMAIL PROTECTED] Getting a result I don't understand. Why does the non-existent key in the array below evaluate to the first char of the array value? $foo = '' ; $foo['one']['two'] = 'test-value' ; // Evaluates to: string(1) t var_dump( $foo['one']['two']['three'] ) ; // Evaluates to NULL, as expected var_dump($foo['one']['two']['three']['four'] ) ; Can anyone enlighten me? Strings are arrays. PHP probably takes the string 'three' and converts it to a integer to see what key of the string you're requesting. $foo = 'bar'; echo $foo[1]; //should echo 'a' Does that help or do you need a deeper explanation (that I probably can't provide!) ;) ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Odd behaviour of non-existent keys?
John, Why does the non-existent key in the array below evaluate to the first char of the array value? $foo = '' ; $foo['one']['two'] = 'test-value' ; // Evaluates to: string(1) t var_dump( $foo['one']['two']['three'] ) ; JH Strings are arrays. PHP probably takes the string 'three' and converts it to JH a integer to see what key of the string you're requesting. JH $foo = 'bar'; JH echo $foo[1]; //should echo 'a' JH Does that help or do you need a deeper explanation (that I probably can't JH provide!) ;) I think you are probably right - but this behaviour causes problems. For example: $foo['one']['two'] = test-string ; // Evaluates to TRUE (not what's wanted!) isset( $foo['one']['two']['three'] ) ; I need a reliable way to test for the non-existence of a multi-dimensional key. Any ideas? I guess I could convert the keys into a string via a loop and compare that, but there must be a more elegant way, surely? -- Geoff Caplan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Odd behaviour of non-existent keys?
On Wed, 25 Aug 2004 14:04:51 +0100, Geoff Caplan [EMAIL PROTECTED] wrote: I think you are probably right - but this behaviour causes problems. For example: $foo['one']['two'] = test-string ; // Evaluates to TRUE (not what's wanted!) isset( $foo['one']['two']['three'] ) ; I need a reliable way to test for the non-existence of a multi-dimensional key. Any ideas? I guess I could convert the keys into a string via a loop and compare that, but there must be a more elegant way, surely? -- Geoff Caplan How about array_key_exists? array_key_exists('three', $foo['one']['two']); http://jp2.php.net/manual/en/function.array-key-exists.php ramil http://ramil.sagum.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Odd behaviour of non-existent keys?
From: Geoff Caplan [EMAIL PROTECTED] Why does the non-existent key in the array below evaluate to the first char of the array value? $foo = '' ; $foo['one']['two'] = 'test-value' ; // Evaluates to: string(1) t var_dump( $foo['one']['two']['three'] ) ; JH Strings are arrays. PHP probably takes the string 'three' and converts it to JH a integer to see what key of the string you're requesting. JH $foo = 'bar'; JH echo $foo[1]; //should echo 'a' JH Does that help or do you need a deeper explanation (that I probably can't JH provide!) ;) I think you are probably right - but this behaviour causes problems. For example: $foo['one']['two'] = test-string ; // Evaluates to TRUE (not what's wanted!) isset( $foo['one']['two']['three'] ) ; I need a reliable way to test for the non-existence of a multi-dimensional key. Any ideas? I guess I could convert the keys into a string via a loop and compare that, but there must be a more elegant way, surely? How about is_array()? if(is_array($foo['one']['two'])) { echo 'multi dimensional array found'; } You also have is_string(), empty(), etc... ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: Re[2]: [PHP] Odd behaviour of non-existent keys?
Geoff Caplan wrote: I think you are probably right - but this behaviour causes problems. For example: $foo['one']['two'] = test-string ; // Evaluates to TRUE (not what's wanted!) isset( $foo['one']['two']['three'] ) ; I need a reliable way to test for the non-existence of a multi-dimensional key. Any ideas? I guess I could convert the keys into a string via a loop and compare that, but there must be a more elegant way, surely? IMHO what you have described is a bug in PHP, and if I were you, I'd report it as such. If it's not a bug it at least has a very high WTF factor. Having to use the cumbersome: is_array($foo['one']['two']) array_key_exists('three', $foo['one']['two']) instead of: isset($foo['one']['two']['three']) seems kinda silly to me. I doubt that this is the intended behavior. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re [PHP] Odd behaviour of non-existent keys?
Michael, MS IMHO what you have described is a bug in PHP, and if I were you, I'd report it as MS such. If it's not a bug it at least has a very high WTF factor. You are right - it does seem like a bug. But I assumed that something as basic as this would have been spotted by now. I don't suppose it would do any harm for me to report it, though, now there are at least 2 of us that think it looks odd. -- Geoff Caplan Vario Software Ltd (+44) 121-515 1154 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php