4

Without specifically doing so,

Like:

function init($var){
  $var = 'x';
}

function a(){
  init($foo);

  echo $foo; // should be x

}

Something like the list() function :)

2 Answers 2

11

Pass by reference:

function init(&$var){
  $var = 'x';
}
Sign up to request clarification or add additional context in comments.

5 Comments

I had the link I edited into your answer in my clipboard in preparation for my answer. Seeing as you've answered this already, I decided to put it to some use.
thanks, didn't think of that :p Can the &$var argument be optional? Because I tried to check if the argument is false and it doesn't seem to assign it
@Alex: How would it be optional and still have it assigned a value?
I mean, if it's not present, then don't create the variable :)
@Alex - Provide a default value of null for the parameter: function init(&$var = null).
2

Sure. You can pass the parameter by reference. So you have to change this

function init($var)

into this:

function init(&$var)

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.