|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-26 19:49 UTC] Ultrasick at gmx dot de
Description: ------------ <? array_pop($DoesntExist); ?> leads to: "Warning: array_pop() [function.array-pop]: The argument should be an array in /.../test.php on line 2" But documentation says: "If array is empty (or is not an array), NULL will be returned." Reproduce code: --------------- <? array_pop($DoesntExist); ?> Expected result: ---------------- should return NULL Actual result: -------------- "Warning: array_pop() [function.array-pop]: The argument should be an array in /.../test.php on line 2" PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 22 05:00:01 2025 UTC |
If you use the array_pop command in a loop like this: <? $Objects[] = 'foo'; $Objects[] = 'bar'; $Objects[] = 'foobar'; while($Object = array_pop($Objects)){ # ... } ?> It wouldn't emit a warning message. The difference between an "empty array" and an array which doesn't exist is so minimal. In my opinion there is no good reason why the first case shouldn't emit a warning while the second does. But yes, it's true. The loop actually stops because when the warning is emited NULL is beeing returned. It shouldn't be that php usually is set to generate a warning message and the user has to throw away the warning message again (@array_pop).>It should bring a warning message if the parameter is a variable but >not >an array and not an unset array (like the $DoesntExist parameter). No, if $DoesntExist is not declared then it is NULL, PHP does NOT enforce variable declaration. and array_pop(NULL) obviously has to emit a warning because NULL ==! array ;-) [quote] If you use the array_pop command in a loop like this: <? $Objects[] = 'foo'; $Objects[] = 'bar'; $Objects[] = 'foobar'; while($Object = array_pop($Objects)){ # ... } ?> [quote] of course that code wont emit a warning at all, and that is expected. when you do $Objects[] = 'foo'; $Objects is being initialized and one element, with key '0' and value 'foo' is added, nothing surprising at all... This is only a documentation problem