From: peillert at xs4all dot nl
Operating system: Linux
PHP version: 5.2.6
PHP Bug Type: Variables related
Bug description: Array's don't convert as per documentation
Description:
------------
Array's don't convert as the documentation says and emit's an E_WARNING
upon conversion.
the documentation says that when converting an scalar value to an array
the result will be an array with 1 element, the original scalar value.
however the scalar value is never converted and an E_WARNING is emitted
instead. see code below.
a quote from the documentation:
"For any of the types: integer, float, string, boolean and resource,
converting a value to an array results in an array with a single element
with index zero and the value of the scalar which was converted. In other
words, (array)$scalarValue is exactly the same as array($scalarValue)."
more interesting is that also the '(array)$scalarValue' does not work but
if the scalar evaluates to NULL such as false or an empty string it does
work. as in the test code i including below only test 2 works as expected.
test 1 emits an E_WARNING and test 3 fails silently.
now either the documentation is out-of-date but i cannot imagine that this
is the intended behaviour. especially test 3 is troublesome as an explicit
cast of any kind should not fail silently.
Reproduce code:
---------------
<?php
error_reporting(E_ALL);
print("\n Test 1.\n");
$scalar = true;
var_dump($scalar);
$scalar[] = "A new element to the array";
var_dump($scalar);
print("\n Test 2. \n");
$scalar = false;
var_dump($scalar);
$scalar[] = "A new element to the array";
var_dump($scalar);
print("\n Test 3. \n");
$scalar = true;
var_dump($scalar);
(array)$scalar;
var_dump($scalar);
?>
Expected result:
----------------
//test 1.
bool(true);
array(1) {
[0] =>
bool(true);
[1] =>
string(26) "A new element to the array";
}
//for test 2.
bool(false);
array(1) {
[0] =>
string(26) "A new element to the array";
}
//for test3.
(bool)true;
array(1) {
[0] =>
bool(true);
}
Actual result:
--------------
Test 1.
bool(true)
Warning: Cannot use a scalar value as an array in array_bug.php on line 6
bool(true)
Test 2.
bool(false)
array(1) {
[0]=>
string(26) "A new element to the array"
}
Test 3.
bool(true)
bool(true)
--
Edit bug report at http://bugs.php.net/?id=45319&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=45319&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=45319&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=45319&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=45319&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=45319&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=45319&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=45319&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=45319&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=45319&r=support
Expected behavior: http://bugs.php.net/fix.php?id=45319&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=45319&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=45319&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=45319&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=45319&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=45319&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=45319&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=45319&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=45319&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=45319&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=45319&r=mysqlcfg