-1

In a PHP function, I'd like to append values to an array that was passed in by reference. For example:

function foo(array &$arr) {
    $arr[] = "error on this line";
    $arr[] = "more";
    $arr[] = "stuff";
}

The error I get when I attempt to append something to the array is

PHP Parse error:  syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING in somefile.class.php on line xxx

I'm not sure I have the parameter defined correctly, or if this is even possible. Googling has so far not turned up any comparable examples.

EDIT: PHP Version 5.1.6.

9
  • 1
    Is $arr declared as array? Commented Aug 4, 2015 at 20:35
  • 1
    I tried the function and it is working without any errors Commented Aug 4, 2015 at 20:39
  • 1
    Your example function works just fine. Please expand your example code to a reproducible error. Commented Aug 4, 2015 at 20:46
  • 3
    I can't reproduce the error with 5.1.6, sandbox.onlinephpfunctions.com/code/…. Commented Aug 4, 2015 at 20:57
  • 1
    Looks like an error a dozen lines before it caused the parsing error. But I'm happy to know about array_push(). Commented Aug 4, 2015 at 21:08

3 Answers 3

2

I just ran your code and got the expected output, so I'm not sure what's happening. If arr wasn't an array, it wouldn't pass the typehint, so it's definitely an array.

It's possible that you're running PHP < 5.4

<?php

function foo(array &$arr) {
    $arr[] = "error on this line";
    $arr[] = "more";
    $arr[] = "stuff";
}

$a = ["initial"];

foo($a);

var_dump($a);

/*
array(4) {
  [0] =>
  string(7) "initial"
  [1] =>
  string(18) "error on this line"
  [2] =>
  string(4) "more"
  [3] =>
  string(5) "stuff"
}
*/

For PHP < 5.4:

<?php

function foo(array &$arr) {
    array_push($arr, "error on this line");
    array_push($arr, "more");
    array_push($arr, "stuff");
}

$a = array("initial");

foo($a);

var_dump($a);

/*
array(4) {
  [0] =>
  string(7) "initial"
  [1] =>
  string(18) "error on this line"
  [2] =>
  string(4) "more"
  [3] =>
  string(5) "stuff"
}
*/
Sign up to request clarification or add additional context in comments.

4 Comments

You can't use short array syntax in PHP < 5.4.
That was the entire point I was trying to make. Just forgot to change the initialisation part as well (edited).
Also, you don't need array_push since $arr[] = is valid PHP 5.3 syntax.
Turns out I'm on 5.1.6.
1

You can use array_push

//The argument '$arr' is declared to be passed by reference, 
 array_push($arr, &$r); 

You can check details here: http://php.net/manual/en/language.references.pass.php

Comments

0

You don't need the "array" keyword before the parameter. The following code works for me:

$arr = array();

function foo(&$arr) {
    $arr[] = "error on this line";
    $arr[] = "more";
    $arr[] = "stuff";
}

foo($arr);

print_r($arr);

Output:

Array ( [0] => error on this line [1] => more [2] => stuff )

EDIT: PHP-version is 5.6

1 Comment

FWIW: While its true that you don't need to use type hints in php, that is not relevant to the error in the question. (I'm sure it also worked on your php with the word "array", so this doesn't tell us anything about why it isn't working for the person asking the question.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.