我希望通过 ref 将 xml 节点传递给函数,并在函数内进行更改。但是似乎无法以这种方式更改节点的值。
例子:
<?php
$str = '<data><name>foo</name></data>';
$xml = simplexml_load_string($str);
test($xml->name);
echo $xml->name; //I expect it should be 'bar', but it is still 'foo'.
function test(&$node){ //it makes no difference if a '&' is added or not.
$node = 'bar';
}
?>
或者如果我在这里犯了错误?