从PHP中的函数返回变量(返回无效) [英] Returning a variable from a function in php (return not working)
问题描述
我正在函数内部构建XML页面,由于某些奇怪的原因,我没有从函数中吐出全部内容.我已经尝试过
I'm building an XML page inside of a function, and for some strange reason I don't get the whole thing spit out of the function. I've tried
return $thisXml;
}
echo $thisXML;
,我只得到函数前变量中的xml声明. 如果我在函数中添加回显,则应按原样返回所有内容.
and I only get the xml declaration which is in the variable before the function. If i put an echo in the function, i get everything back as I should.
我的页面基本上是这样的
my page essentially looks like this
$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;
正如我所说,如果我将'return'替换为'echo',则会得到所有不错的xml.如果我在函数外回显,则只会得到原始的声明.
as i said, if I replace the 'return' with 'echo', I get all the nice xml. if I echo outside the function, I only get the original declaration.
真的很奇怪,我整天都在为此苦苦挣扎.
really strange, and i've been struggling with this one all day.
推荐答案
return $thisXml;
}
echo $thisXML;
$ thisXML;仅存在于功能范围内.
要么使$ thisXML;全局(不好的主意)或echo getThisXML()
,其中getThisXML是返回$thisXML
;
$thisXML; only exists in the scope of the function.
Either make $thisXML; global (bad idea) or echo getThisXML()
where getThisXML is the function that returns $thisXML
;
这篇关于从PHP中的函数返回变量(返回无效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!