我知道其他队友已经提供了解决方案,所以我在未来方面加我的答案。
假设你有两个功能getHello()
和getGoodbye()
具有不同定义的相同目的。
// function one
function getHello(){
return "Hello";
}
// function two
function getGoodbye(){
echo "Goodbye";
}
//now call getHello() function
$helloVar = getHello();
结果:
'Hello' // return 'hello' and stored value in $helloVar
//now call getGoodbye() function
$goodbyeVar = getGoodbye();
结果:
'Goodbye' // echo 'Goodbye' and not stored in $goodbyeVar
echo $helloVar; // "Hello"
echo $goodbyeVar; // Goodbye
结果:
'GoodbyeHello'
// now try same example with this:
echo $helloVar; // "Hello"
//echo $goodbyeVar; // Goodbye
结果应该相同,因为getGoodbye()
已经是echo'ed
的结果。
现在实例与您的代码:
function myfunction($test){
//global $test;
echo $test;
}
function myfunction2($test){
//global $test;
return $test;
}
myfunction('test'); // test
myfunction2('test'); // noting
//You need to echo myfunction2() as i mentioned in above.
echo myfunction2('test'); // test
为什么它不是在你的代码?:
工作,你需要像指派值之前声明变量Global
:
global $test;
$test = "blah";
是你的'myfunction'类的一部分吗?或只是简单的书面功能 – urfusion
@urfusion不,这是一个简单的功能 – Pedram
你确定带有功能的文件被正确包含吗? – RamRaider