我正在一个函数内部构建一个 XML 页面,但出于某种奇怪的原因,我没有从函数中取出所有内容。我试过了
return $thisXml;
}
echo $thisXML;
我只得到函数之前变量中的 xml 声明。 如果我在函数中放一个 echo ,我会按我应该的方式取回所有内容。
我的页面基本上是这样的
$thisXml = 'xml declaration stuff';
function getThisXML($thisXML){
for(i=1; i<5; i++){
$query "has the 5 in it";
while ($mysqlQuery =mysql_fetch_array($theQuery) {
$thisXml.='add the xml';
}
$thisXml.='close the last element';
return $thisXml;
}
echo $thisXml;
正如我所说,如果我用“echo”替换“return”,我会得到所有漂亮的 xml。如果我在函数外回显,我只会得到原始声明。
真的很奇怪,我整天都在为这个问题苦苦挣扎。
最佳答案
return $thisXml;
}
echo $thisXML;
$这个XML;只存在于函数的范围内。
要么制作 $thisXML;全局(坏主意)或 echo getThisXML()
其中 getThisXML 是返回 $thisXML
的函数;
关于php - 从 php 中的函数返回变量(返回无效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/513511/