ID:               40692
 User updated by:  smlerman at gmail dot com
 Reported By:      smlerman at gmail dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Any
 PHP Version:      5.2.1
 New Comment:

Here's another test case that shows that something isn't quite right
with implicit conversions.

<?php

var_dump(error_reporting());
$a = 12345;
var_dump($a[0]); // Should cast to string or possibly array
var_dump($a{0}); // Should cast to string

$b = (string)$a;
var_dump($b[0]);

$c = (array)$a;
var_dump($c[0]);

?>

Results:

int(8191)
NULL
NULL
string(1) "1"
int(12345)

So $a isn't being converted to a string or array if you do $a[0]. It
also isn't converted to a string if you do $a{0}, and I have no idea
what else it could reasonably convert to. I never rely on these
implicit conversions, so I have no real personal interest in whether
the behavior stays the same as it is now or if the conversions are
fixed, but some kind of error message, even a notice for an undefined
index for something like $a['foo'], would help with debugging this kind
of programmer mistake.


Previous Comments:
------------------------------------------------------------------------

[2007-03-05 00:22:42] smlerman at gmail dot com

Actually, as my code shows, you do not get false, you get NULL, so it's
obviously not doing a normal conversion to an array. I'm not disputing
the value of the expression, since it makes perfect sense to me that
the value of a non-existent variable should be NULL. In all other
cases, though, it also gives a notice, which is what would be nice to
have.

------------------------------------------------------------------------

[2007-03-05 00:10:28] [EMAIL PROTECTED]

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

(string)false == ""
"" does not have offset 0 and therefor you get a warning message.

(array)false == array(0 => false);

and when you access element 0 of this array you get false in return.

------------------------------------------------------------------------

[2007-03-03 16:57:17] smlerman at gmail dot com

Yeah, in 6 years of programming PHP, I never noticed that it isn't a
strictly typed language. Trying to use an undefined offset of an array
gives an error message. Trying to use a non-existant character index of
a string gives an error message. So what is the boolean being converted
to such that using an incorrect offset isn't an error?

------------------------------------------------------------------------

[2007-03-03 15:58:22] [EMAIL PROTECTED]

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

PHP is not type strict.

------------------------------------------------------------------------

[2007-03-02 16:20:40] smlerman at gmail dot com

Description:
------------
If you try to use a boolean as an array (which most likely means an
error occurred somewhere), the value is correctly returned as NULL, but
no error message is reported. Obviously not a major problem, but it
would make debugging a little easier.

Reproduce code:
---------------
<?php

var_dump(error_reporting());
$a = false;
var_dump($a[0]);

$b = (string)$a;
var_dump($b[0]);

?>

Expected result:
----------------
int(8191)
[Something like] Notice: Cannot use boolean as array in C:\Documents
and Settings\...\boolean_array.php on line 5
NULL

Notice: Uninitialized string offset:  0 in C:\Documents and
Settings\...\boolean_array.php on line 8
string(0) ""

Actual result:
--------------
int(8191)
NULL

Notice: Uninitialized string offset:  0 in C:\Documents and
Settings\Scott\My Documents\Test Files\boolean_array.php on line 8
string(0) ""


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=40692&edit=1

Reply via email to