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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 12:01:27 2024 UTC