0
$cars = array(
     array('name' => 'Toyota', price => 10000, ...),
     array('name' => 'Ford', price => 20000, ...),
     ...
);

foreach($cars as &$car)
{
   do_something($car['name']);
}
unset($car);

function do_something($name)
{
    ....
}

In do_something() function, is $name a reference or a value? If it is a reference, how do I just retrieve the value in the loop and pass that in the function?

1 Answer 1

3

No, $name in do_something() function is not a reference, it is not pass by reference, only pass the value of $car['name'] to $name.

If you want to pass by reference, you could do like below, pass $car as reference.

$cars = array(
     array('name' => 'Toyota', price => 10000),
     array('name' => 'Ford', price => 20000)
);

foreach($cars as &$car)
{
   do_something($car);
}

var_dump($cars);

function do_something(&$car)
{
    $car['name'] .= '_changed';
}
Sign up to request clarification or add additional context in comments.

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.