7

Why doesn't this work the way I think it should?

I have an array and I'd like to modify one of its values with a function.

I've been reading and following some tutorials and think it has to do with the variable scope? Or maybe this is just not the way to approach something like this and should use other methods?

<?php
$someArray = array("value1"=> 0, "value2" => 0);
function test ($a) {
    if ( 5 > 4 ) {
        $a["value1"] += 1;
        echo $a["value1"] . "<br/>";
    }
}
test($someArray);
echo $someArray["value1"];
?>

I don't get why it works when I echo inside the function to get the new value of "value1", but outside it doesn't work.

4 Answers 4

14

You are passing as a copy of the array. You should pass the array using the address to reflect the changes done inside the array. Use & (passing as reference):

$someArray = array("value1"=> 0, "value2" => 0);
function test (&$a) {   //Use & here
               ^
    if ( 5 > 4 ) {
        $a["value1"] += 1;
        echo $a["value1"] . "<br/>";            
    }
}
test($someArray);
echo $someArray["value1"];

Here is the Explanation: (fetched from here)

explanation

Read this SO question too.


Other way is to return the value from function. Inside the function, use return and capture it outside:

$someArray = array("value1"=> 0, "value2" => 0);
function test ($a) {
    if ( 5 > 4 ) {
        $a["value1"] += 1;
        echo $a["value1"] . "<br/>";            
    }
    return $a; //Return here
}
$someArray = test($someArray);  //Capture here
echo $someArray["value1"];
Sign up to request clarification or add additional context in comments.

2 Comments

So many years of PHP coding (for WordPress). Never ever wrote any &$arg pass by reference code. I always thought of it as some optional thing, never ever had any issues, I just do not write code like that. I think it's more an issue for people coming from other programming languages with pointers and shit. I just always return things and work with that. I do not like this passing by reference at all, it just makes code harder to understand. So my guess is, passing by reference is more memory efficient, no copy into new var, but you should never think like that.
@redanimalwar I agree, but don't forget that anytime you pass an object you're passing by reference.
2

Try:

1) return updated array $a from function

2) receive that array in $someArray again

$someArray = array("value1"=> 0, "value2" => 0);
function test ($a) {
    if ( 5 > 4 ) {
        $a["value1"] += 1;
    }
    return $a;
}
$someArray = test($someArray);
echo "Updated ".$someArray["value1"];

Comments

1

Use pass by reference

test(&$someArray); 

Comments

1

Your function echoes stuff, but it does not alter or return the value. You want to change/alter the value, so you can return this value instead of echo in your function and set a variable for that value to echo out. Below, you can find how you should do;

<?php
$someArray = array("value1"=> 0, "value2" => 0);
function test ($a) {
    if ( 5 > 4 ) {
        $a["value1"] += 1;
        return $a["value1"] . "<br/> YEY we did it";            
    }
}
echo test($someArray);
?>

And check it here in a sandbox.

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.