php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45319 Array's don't convert as per documentation
Submitted: 2008-06-20 01:59 UTC Modified: 2016-08-12 12:06 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: peillert at xs4all dot nl Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: peillert at xs4all dot nl
New email:
PHP Version: OS:

 

 [2008-06-20 01:59 UTC] peillert at xs4all dot nl
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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-20 13:23 UTC] smlerman at gmail dot com
I'm not sure, but this may be related to a bug I filed a while ago: http://bugs.php.net/bug.php?id=40692
 [2008-06-21 02:54 UTC] peillert at xs4all dot nl
It's not the same but it may be caused by the same bug. they seem to be related yet not identical. an E_WARNING or E_NOTICE should be emitted if any cast, implicit but certainly explicit fails. 

php may not be type-strict but in cases where implicit conversion makes no sense such as accessing an scalar as if it where an array should emit an E_NOTICE telling the developer about his/her illogical use of the variable. and i believe it does if the scalar gets converted right then it should emit an undefined offset error or something like that.

i thought that was why E_NOTICE was introduced. to tell the developer that what he/she is trying to do is not a good coding practice.
 [2008-06-23 13:52 UTC] m dot sedziwoj at gmail dot com
I think is misunderstand in understand the documentation, test it:

<?php

$scalar = true;
var_dump($scalar);

$array_from_scalar = (array)$scalar;
var_dump($scalar,$array_from_scalear);

?>
It convert, not overwrite.
 [2016-08-12 12:06 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug -Package: Feature/Change Request +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2016-08-12 12:06 UTC] cmb@php.net
Casting to array works as expected. Wrt. to using the index
operator on invalid containers, there is alread request #37676.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC