Function that change args with php?
I dont know what we call this, but it will be something like this
$args = 开发者_如何学运维1
function rendercomments($args,$usersholder = NULL ){
$args = $args + 1;
// missing
return true;
}
changeargs($args);
$userholder = 2
Can we do something like that? Someway to make $args append to $usersholder without returning it?
Yes, all you need is to define your arguments "by reference" by using the ampersand (&
).
function rendercomments(&$args, $usersholder = NULL){
$args += 1;
return true;
}
$args = 1;
rendercomments($args);
echo $args; // 2
I believe what you are asking for is to update the $userholder
variable while inside the called function.
If so, you have two options:
- Create a class that contains the
$userholder
and thechangeargs($args);
should be inside that class and you can just use the$this
keyword to apply any changes to the$userholder
object and the changes will continue outside of thechangeargs()
function. - Pass into the
changeargs()
function a reference to the$userholder
variable so that you are modifying that variable the whole time.
精彩评论