php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34772 Weird type juggling problem (may not be a bug at all)
Submitted: 2005-10-07 06:37 UTC Modified: 2005-10-07 15:13 UTC
From: rn214 at cam dot ac dot uk Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.4.0 OS: Linux
Private report: No CVE-ID: None
 [2005-10-07 06:37 UTC] rn214 at cam dot ac dot uk
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)



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-07 09:36 UTC] sniper@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Try var_dump() on it..

 [2005-10-07 15:13 UTC] rn214 at cam dot ac dot uk
Sorry to trouble you. The behaviour does seem wrong to me, so I thought it was better to report it as a possible bug than to ignore it.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 17:01:32 2025 UTC