0

I have the following scenario (simplified):

function changeFruit($fruit) {
    changeAgain($fruit);

}

function changeAgain($fruit) {
     $fruit = "Orange";
}

MAIN:

$fruit = "Apple";
changeFruit($fruit);
echo $fruit // Will show up as "Apple", How do I get it to show up as "Orange"??

EDIT: FORGOT TO ADD. THE SCENARIO CANNOT USE RETURN STATEMENTS - JUST &$variable

Thanks! Matt Mueller

4
  • 2
    To the person who downvoted the question, you should seriously go back and check your very first code. Commented Oct 20, 2009 at 10:18
  • 3
    And also, good practice to leave a comment if you downvote a question, to help improve the question) Commented Oct 20, 2009 at 10:22
  • Anax: It wasn't me who downvoted you, but it seems that you haven't read much of the manual. Try this: php.net/manual/en/functions.arguments.php Commented Oct 20, 2009 at 12:59
  • Hey I don't want to start a flame war here.. the issue DID NOT involve return statements.. I wanted more information on &$variable. I apologize it was late and a poorly written question and description, but my question's intent was not entirely trivial. Commented Oct 21, 2009 at 0:09

3 Answers 3

10

When you pass something that is not an object to a function in PHP, php makes a copy of that to use within the function.

To make it not use a copy, you need to tell PHP you are passing a reference.

This is done with the & operator

function changeFruit(&$fruit) {
    changeAgain($fruit);

}

function changeAgain(&$fruit) {
     $fruit = "Orange";
}

$fruit = "Apple";
changeFruit($fruit);
echo $fruit;

It would be more sensible, and better practice, to use return values of the functions (as this makes things easier to read)

function changeFruit($fruit) {
    return changeAgain($fruit);
}

function changeAgain($fruit) {
     // do something more interesting with$fruit here
     $fruit = "Orange";
     return $fruit;
}

$fruit = "Apple";
$fruit = changeFruit($fruit);
echo $fruit
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for digging into the question a little more. I forgot to add, that I can't return values!
I was about to write exactly this. Entirely correct and very complete. Clean answer and clear examples!
Thanks Paul - I tend to only answer if I'm not going to be giving one liners, and there's something I can go into in detail regarding :D
2
function changeFruit($fruit) {
    return changeAgain($fruit);

}

function changeAgain($fruit) {
     return $fruit = "Orange";
}

MAIN:

$fruit = "Apple";
$fruit = changeFruit($fruit);
echo $fruit;

Hope that helps!

Note: the return from the changeAgain function and overwriting $fruit = changeFruit($fruit);

1 Comment

changeFruit also needs to return
1

You are not returning the values from your functions. Try this:

function changeFruit($fruit) {
    return changeAgain($fruit);

}

function changeAgain($fruit) {
     $fruit = "Orange";
     return $fruit;
}

MAIN:

$fruit = "Apple";
$fruit = changeFruit($fruit);

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.