开发者

Function to create new, previously undefined variable

开发者 https://www.devze.com 2023-01-29 08:20 出处:网络
Sorry for such a lame title, but I just had no idea what to put there, hope you understand it. Plus, I have no idea if similar question has been asked before, because I don\'t know the proper keywords

Sorry for such a lame title, but I just had no idea what to put there, hope you understand it. Plus, I have no idea if similar question has been asked before, because I don't know the proper keywords for it - therefore couldn't google it too.

Basicly... When looking at preg_match_all();, they got this matches parameter that will create new array defined in function, and give us the ability to access it after function execution.

And the question is.. How can I implement such a feature in my own function? But that it could create single variable and/or array.

开发者_如何学Go

Thanks in advance!


preg_match_all() accepts a reference to an array, which in its own scope is called $matches. As seen in the function prototype:

array &$matches

If you call the function and pass in a variable, if it does not already exist in the calling scope it will be created. So in your user-defined function, you accept a parameter by reference using &, then work with it inside your function. Create your outer-scope variable by simply declaring it in your function call, like you the way you call preg_match_all() with $matches.

An example:

function foo(&$bar) {
    $bar = 'baz';
}

// Declare a variable and pass it to foo()
foo($variable);
echo $variable; // baz


I think you are referring to function parameters passed by reference, are you not?

function putValInVar(&$myVar, $myVal){
    $myVar = $myVal;
}

$myVar = 1;
putValInVar($myVar, 2);

echo $myVar; // outputs '2', but will output '1' if we remove the '&' //

By default function arguments in PHP are passed by value. This means that new variables are created at each function call and those variables will exist only inside the function, not affecting anything outside it.

To specify that an argument should be used by reference the syntax is to append an & before declaring it in the function header. This will instruct PHP to use the passed variable inside the function rather than creating a copy of it.

Exception: Objects are always passed by reference. (Well... Not really, but it's complicated. See the comment thread for more info.)


I think what you are asking for is passing-by-reference. What preg_match_all basically does to "create" an array variable outside its scope is:

 function preg_match_all($foo, $bar, & $new_var) {
     $new_var = array(1,2,3);
 }

The crucial point here is & in the function definition. This allows you to overwrite variables in the outer scope when passed.

Stylistically this should be used with care. Try to return arrays or results instead of doing it via reference passing.


Like this:

$myvariable = runfunction();

function runfunction() {
//do some code assign result to variable (ie $result)
return $result;
}

Or

global $result;
function runfunction() {
global $result;
$result = 'something';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消