|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-03-02 16:20 UTC] 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) "" PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 20:00:01 2025 UTC |
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.As has already been explained, the behaviour exhibited is expected. Not only is it expected it is the only reasonable thing _to expect_. The only time you can expect a type to be coerced is upon passing it to the engine _for manipulation_ or upon explicit casting: <?php var_dump(error_reporting()); $a = false; var_dump($a[0]); $b = (string)$a; var_dump($b[0]); ?> If the engine had cast $a to anything on line 2, _that_ would be unexpected; if it had been done as if by magic then PHP would be unusable (because you would never know what type any variable was from one line to the next), if it had been done by the call to var_dump implicitly then var_dump would be useless for it's intended purpose. PHP is not strictly typed, end of story :) Closing request ...