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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/513511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:56:56  来源:igfitidea点击:

Returning a variable from a function in php (return not working)

php

提问by pedalpete

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.

正如我所说,如果我用“回声”替换“返回”,我会得到所有漂亮的 xml。如果我在函数外回显,我只会得到原始声明。

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

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

回答by Steerpike

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

回答by jeroen

Are you actually calling the function in the sense of:

您实际上是在以下意义上调用该函数:

$thisXml = getThisXML($someinput);

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

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

回答by Matias

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;

回答by Gumbo

You have to call the function and apply echoon the returned value:

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

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

Or you pass the variably by reference.

或者您通过引用传递变量。

回答by Jorge Niedbalski R.

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

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

Use:

用:

$thisXML;

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

print $thisXML;

回答by Facebook Staff are Complicit

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

回答by Hamid Ghp

You can create function in php this way:

<?php

$name = array("ghp", "hamid", "amin", "Linux");
function find()
{
    $find = 0;
    if(in_array('hamid', $name))
    {
      $find = 1;
      return $find;
    }
    else 
    {
      return $find;
    }
}


//###################
$answare = find();
echo $answare;
?>