0

I've the following code function:

function foo(&$vett) {

    $vettore = $vett;

    $vettore[] = "ciao";

    var_dump($vettore);
}

$v = array();

foo($v);

var_dump($v);

When I dump the final array is empty. Have you any idea of what could be?

5
  • What is passed by reference here? Commented Jun 21, 2018 at 7:04
  • Now I fixed the question Commented Jun 21, 2018 at 7:05
  • So if you pass something by reference - what's the point of assigning passed value to another one? Commented Jun 21, 2018 at 7:05
  • stackoverflow.com/questions/2030906/… may be useful Commented Jun 21, 2018 at 7:17
  • When you fixed it, you can post your own answer and accept it. Beside the reputation, your solution may be of some help for other users. Commented Jun 21, 2018 at 7:23

2 Answers 2

1

Because $v never modified. Inside the function you assign the variable into another variable. So nothing ever happen to the old $vett

try something like:

function foo(&$vett) {
    $vett[] = "ciao";
    echo __LINE__;
    var_dump($vett);
}

$v = array();
foo($v);
var_dump($v);
Sign up to request clarification or add additional context in comments.

Comments

0

Correct version is:

function foo(&$vett) {
    $vett[] = "ciao";
}

$v = array();
foo($v);
var_dump($v);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.