开发者

reference variable array

I always use $text = $datasql[0]; where $datasql = array('0'=>array('some'=>'text', 'some2'=>'text2'), '1'=>$data, etc...);

and found work construction $datasql = &$datasql[0]; and work, why?

That really开发者_高级运维 reference?? and how remember php in memory this solution.


Every variable is a reference to a value. Normally the value is copied when you use it, but with & the reference is copied.

Suppose you have the following variable:

$original = 'john';

If you assign the value from $datasql to a variable, that value is copied:

$text = $original;

If you assign a reference, the value is not copied but referenced:

$text = & $original;

This means that $text points to the value of $original. Now, if you unset $original, the contents of $text are still valid:

unset($original);
echo $text; // john

This is because PHP knows there is still a reference to the value of $original, so it deletes the $original variable as name, but not the contents.

Your example is similar, except that the variable is not explicitly unset, but overwritten. It is a reference to a value, just like any other variable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜