从php中的函数返回变量(返回不工作)

ped*_*ete 7 php

我正在一个函数内部构建一个XML页面,由于一些奇怪的原因,我没有把整个东西吐出函数.我试过了

return $thisXml;
}
echo $thisXML;
Run Code Online (Sandbox Code Playgroud)

我只获得函数前变量中的xml声明.如果我在函数中放置一个回声,我就会得到所有的回报.

我的页面基本上是这样的

$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;
Run Code Online (Sandbox Code Playgroud)

正如我所说,如果我用'echo'替换'return',我会得到所有不错的xml.如果我在函数外回声,我只得到原始声明.

真的很奇怪,我整天都在为这一天苦苦挣扎.

Ste*_*ike 10

return $thisXml;
}
echo $thisXML;
Run Code Online (Sandbox Code Playgroud)

$ thisXML; 仅存在于函数的范围内.要么$ thisXML; 全局(坏主意)或echo getThisXML()getThisXML是返回的函数$thisXML;


jer*_*oen 7

你实际上是在调用这个函数:

$thisXml = getThisXML($someinput);
Run Code Online (Sandbox Code Playgroud)

也许是一个愚蠢的问题,但我在你的描述中没有看到它.