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

时间:2021-07-31 17:29:00

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

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

return $thisXml;
}
echo $thisXML;

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.

我只获得函数前变量中的xml声明。如果我在函数中放置一个回声,我会尽可能地回复所有内容。

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;

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.

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

really strange, and i've been struggling with this one all day.

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

6 个解决方案

#1


return $thisXml;
}
echo $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;

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

#2


Are you actually calling the function in the sense of:

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

$thisXml = getThisXML($someinput);

$ thisXml = getThisXML($ someinput);

Maybe a silly question, but I don´t see it in your description.

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

#3


You have to call the function and apply echo on the returned value:

您必须调用该函数并对返回的值应用echo:

 $thisXml = '…';
 echo getThisXML($thisXml);

Or you pass the variably by reference.

或者您通过引用可变地传递。

#4


You need to invoke the function!

你需要调用这个功能!

$thisXml = 'xml declaration stuff';

echo getThisXML($thisXML);

Or pass the variable by reference:

或者通过引用传递变量:

$thisXml = 'xml declaration stuff';

function getThisXML(&$thisXML){
  ...
  return $thisXml;
}

getThisXML($thisXML);
echo $thisXml;

#5


You are trying to use a variable defined inside the function scope.

您正在尝试使用函数范围内定义的变量。

Use:

$thisXML;

function do(){
 global $thisXML;
 $thisXML = "foobar";
}

print $thisXML;

#6


Returning a variable doesn't mean that it affects that variable globally, it means the function call evaluates to that value where it's used.

返回变量并不意味着它会全局影响该变量,这意味着函数调用将计算到它所使用的值。

$my_var = 5;

function my_func() {
  $my_var = 10;
  return $my_var;
}

print my_func();
print "\n";
print $my_var;

This will print

这将打印出来

10
5

#1


return $thisXml;
}
echo $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;

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

#2


Are you actually calling the function in the sense of:

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

$thisXml = getThisXML($someinput);

$ thisXml = getThisXML($ someinput);

Maybe a silly question, but I don´t see it in your description.

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

#3


You have to call the function and apply echo on the returned value:

您必须调用该函数并对返回的值应用echo:

 $thisXml = '…';
 echo getThisXML($thisXml);

Or you pass the variably by reference.

或者您通过引用可变地传递。

#4


You need to invoke the function!

你需要调用这个功能!

$thisXml = 'xml declaration stuff';

echo getThisXML($thisXML);

Or pass the variable by reference:

或者通过引用传递变量:

$thisXml = 'xml declaration stuff';

function getThisXML(&$thisXML){
  ...
  return $thisXml;
}

getThisXML($thisXML);
echo $thisXml;

#5


You are trying to use a variable defined inside the function scope.

您正在尝试使用函数范围内定义的变量。

Use:

$thisXML;

function do(){
 global $thisXML;
 $thisXML = "foobar";
}

print $thisXML;

#6


Returning a variable doesn't mean that it affects that variable globally, it means the function call evaluates to that value where it's used.

返回变量并不意味着它会全局影响该变量,这意味着函数调用将计算到它所使用的值。

$my_var = 5;

function my_func() {
  $my_var = 10;
  return $my_var;
}

print my_func();
print "\n";
print $my_var;

This will print

这将打印出来

10
5