#44844 [Opn]: array_pop brings error message if parameter is not an array

From: Date: Mon, 28 Apr 2008 16:22:52 +0000
Subject: #44844 [Opn]: array_pop brings error message if parameter is not an array
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-763@lists.php.net to get a copy of this message
ID: 44844 User updated by: Ultrasick at gmx dot de Reported By: Ultrasick at gmx dot de Status: Open Bug Type: Documentation problem Operating System: Linux PHP Version: 5.2.5 New Comment: I see. If I am right the each() function does also emit a warning message if you pass the "$DoesntExist" as the parameter. So even if I am not happy with that it is at least uniform in php. Previous Comments: ------------------------------------------------------------------------ [2008-04-28 05:34:34] crrodriguez at suse dot de >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 ------------------------------------------------------------------------ [2008-04-27 07:47:56] Ultrasick at gmx dot de 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). ------------------------------------------------------------------------ [2008-04-27 07:43:22] Ultrasick at gmx dot de 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). ------------------------------------------------------------------------ [2008-04-27 02:04:42] crrodriguez at suse dot de This is a docu problem, fails to mention that the that the function emits a warning BTW.. it does return NULL, there is no problem with that. ------------------------------------------------------------------------ [2008-04-26 19:49:35] 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" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=44844&edit=1

« previous php.doc.bugs (#763) next »