开发者

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:

  1. Create a class that contains the $userholder and the changeargs($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 the changeargs() function.
  2. Pass into the changeargs() function a reference to the $userholder variable so that you are modifying that variable the whole time.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜