|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-10-07 09:36 UTC] sniper@php.net
[2005-10-07 15:13 UTC] rn214 at cam dot ac dot uk
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 02:00:02 2025 UTC |
Description: ------------ Something rather weird happens when you define a variable first as a string and then as an array. I am not sure whether this is actually a bug, or my failure to understand null-terminated strings. However, the effect is that the empty-string '' is evaluated as true. Sorry for wasting your time if this is spurious. Reproduce code: --------------- $foo[1]="x"; $foo=$foo[1]; $foo[2]=''; if ($foo[1]){ echo "foo 1 is '$foo[1]' and is true<br>"; } if ($foo[2]){ #The value of $foo[2] printed here is \0. It evalutes as true, and is a 1-character length string. #This is DESPITE that fact that I just set it to be empty! echo "foo 2 is '$foo[2]' and is true<br>"; } #A simpler case: $bar="x"; $bar[2]=''; echo "bar 2 is '$bar[2]'<br>"; Expected result: ---------------- Not quite sure what to expect. But I do not expect the value of foo[2] which I just set to the empty string to evaluate as true! I would expect only to see one line: bar is '' Actual result: -------------- foo 1 is ' ' and is true foo 2 is 'N' and is true bar is 'N' (I have used the character N to represent NUL, since it would otherwise be invisible. Also note the space between the quotes in the first line)