From:             soapergem at gmail dot com
Operating system: WinXP
PHP version:      5.3.0
PHP Bug Type:     Feature/Change Request
Bug description:  Change the behavior of array_fill ever so slightly

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 bug report at http://bugs.php.net/?id=49824&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49824&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49824&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49824&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49824&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49824&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49824&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49824&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49824&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49824&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49824&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49824&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49824&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49824&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49824&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49824&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49824&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49824&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49824&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49824&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49824&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49824&r=mysqlcfg

Reply via email to