php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43017 array_fill() function's inconsistent behavior in PHP5 and PHP6
Submitted: 2007-10-18 10:51 UTC Modified: 2007-11-02 19:44 UTC
From: dharma dot yp at in dot ibm dot com Assigned: jani (profile)
Status: Closed Package: Arrays related
PHP Version: 5.2CVS-2007-10-18 (snap) OS: Windows, Linux
Private report: No CVE-ID: None
 [2007-10-18 10:51 UTC] dharma dot yp at in dot ibm dot com
Description:
------------
1) In php 5 when following values are given as value for $start_key, the values
are not automatically converted and used,A warning message is displayed. However
automatic conversion of these values happen in php6 and the converted values are 
used, hence no warning messages generated. 
The $start_key values are : 
    null, NULL, true, false, TRUE, FALSE.

The warning message generated in php5 is :

Warning: array_fill(): Wrong data type for start key in %s on line %d
bool(false)

2) In php6 when empty string("" & '') is given as $start_key the array_fill() 
function generates an warning however in php5 it doesn't. conversion happens 
and the value is used.

The warning message generated in php6 is :

Warning: array_fill() expects parameter 1 to be long, string given in %s on line 3
NULL


Please note that documentation doesn't say any thing in specific for these but looking at
array type documentation( http://in.php.net/manual/en/language.types.array.php ), 
an array could have any of these as key. Since this function is doing the creation of 
an array with specified values, it should create array in all possible ways, 
that can be created in general using array(). If not, then one would have to write 
an work-around code when these value are to be passed to array_fill().

Reproduce code:
---------------
1) null and boolean values as $start_key

<?php
var_dump( array_fill(TRUE, 2, 100) );
var_dump( array_fill(NULL, 2, 100) );
?>

2) empty string value as $start_key

<?php
var_dump( array_fill("", 2, 100) );
?>


Expected result:
----------------
1) null and boolean values as $start_key

array(2) {
  [1]=>
  int(100)
  [2]=>
  int(100)
}

array(2) {
  [0]=>
  int(100)
  [1]=>
  int(100)
}

2) empty string value as $start_key

array(2) {
  [0]=>
  int(100)
  [1]=>
  int(100)
}


Actual result:
--------------
1) null and boolean values as $start_key
on php5:
Warning: array_fill(): Wrong data type for start key in %s on line %d
bool(false)


Warning: array_fill(): Wrong data type for start key in %s on line %d
bool(false)

on php6:
array(2) {
  [1]=>
  int(100)
  [2]=>
  int(100)
}

array(2) {
  [0]=>
  int(100)
  [1]=>
  int(100)
}

2) empty string value as $start_key
on php6:

Warning: array_fill() expects parameter 1 to be long, string given in %s on line %d
NULL

Warning: array_fill() expects parameter 1 to be long, string given in %s on line %d
NULL


on php5:
array(2) {
  [0]=>
  int(100)
  [1]=>
  int(100)
}




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-01 10:50 UTC] jani@php.net
Soon it will be consistent once I sync HEAD with PHP_5_3. 5.2 will not be changed though!
 [2007-11-01 12:00 UTC] jani@php.net
And note: the error message is the expected behaviour. If a function expects to get an integer as parameter and you pass it something else -> error.
 [2007-11-02 19:44 UTC] jani@php.net
This is now consistent between 5.3 and 6.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 22:01:33 2024 UTC