Req #49824 [Opn->Csd]: Change array_fill() to allow creating empty array

From: Date: Tue, 12 Nov 2013 21:10:47 +0000
Subject: Req #49824 [Opn->Csd]: Change array_fill() to allow creating empty array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-182724@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49824&edit=1 ID: 49824 Updated by: nikic@php.net Reported by: soapergem at gmail dot com Summary: Change array_fill() to allow creating empty array -Status: Open +Status: Closed Type: Feature/Change Request Package: Arrays related Operating System: * PHP Version: * -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Implemented by https://github.com/php/php-src/commit/dc4bf922d9a0d738b621dff4a55abb715edeee61 for PHP 5.6. Previous Comments: ------------------------------------------------------------------------ [2009-10-09 17:09:06] soapergem at gmail dot com Description: ------------ Right now array_fill() returns false and throws an E_WARNING if its second parameter ($num) is less than 1. This is counter-intuitive. As you know, the purpose of array_fill is to create an array with the given number of elements, all preset to one value. The restriction SHOULD be that $num cannot be less than 0, rather than saying it cannot be less than 1. Why? Because an array of zero-length is a perfectly valid thing in PHP. I ran into a scenario where I was using array fill and $num was a dynamic value. I would have never expected that PHP would actually throw a warning if I told it to create an empty array. I had to write a wrapper function to accommodate, which is stupid. Just allow me to create an empty array like any reasonable person would expect! Reproduce code: --------------- <?php $array1 = array_fill(0, -1, 1); $array2 = array_fill(0, 0, 1); $array3 = array_fill(0, 1, 1); var_dump($array1); var_dump($array2); var_dump($array3); ?> Expected result: ---------------- bool(false) array(0) { } array(1) { [0]=> int(1) } PHP Warning: array_fill(): Number of elements must be nonnegative in C:\test.php on line 3 Actual result: -------------- bool(false) bool(false) array(1) { [0]=> int(1) } PHP Warning: array_fill(): Number of elements must be positive in C:\test.php on line 3 PHP Warning: array_fill(): Number of elements must be positive in C:\test.php on line 4 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=49824&edit=1

« previous php.bugs (#182724) next »