php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44844 array_pop brings error message if parameter is not an array
Submitted: 2008-04-26 19:49 UTC Modified: 2008-05-02 21:25 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Ultrasick at gmx dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
 [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"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-27 02:04 UTC] 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-27 07:43 UTC] 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 07:47 UTC] 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-28 05:34 UTC] 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-28 16:22 UTC] Ultrasick at gmx dot de
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.
 [2008-05-02 21:25 UTC] fa@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation
better.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 10:01:32 2024 UTC