php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65330 Attaching a variable to an array within the return construct doesn't work
Submitted: 2013-07-25 08:41 UTC Modified: 2013-07-25 13:44 UTC
From: fausten at pw-internet dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.17 OS: FreeBSD 9.1
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fausten at pw-internet dot de
New email:
PHP Version: OS:

 

 [2013-07-25 08:41 UTC] fausten at pw-internet dot de
Description:
------------
If you try within a return construct to attach a variable to an array, the array will be overwriten by the value you tried to attach to it. Because the following examples work, it should also work with test script due to consistency.

$ php -r 'function foo() { $intNumber = 0; return $intNumber + 1 + 2; } var_dump(foo());'
int(3)

$ php -r 'function bar() { $strText = "foo"; return $strText . "bar"; } var_dump(bar());'
string(6) "foobar"

Test script:
---------------
$ php -r 'function foo() { $arrFoo = array(); return $arrFoo[] = "foo"; } var_dump(foo());'

Expected result:
----------------
array(1) {
  [0]=>
  string(3) "foo"
}

Actual result:
--------------
string(3) "foo"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-25 13:44 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2013-07-25 13:44 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I think you should use 

function foo() { $arrFoo = array(); $arrFoo[] = "foo"; return $arrFoo; }

$arrFoo[] = "foo" expression result a "foo" as tmp result... so..
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 01:01:29 2025 UTC