PHP将变量传递给另一个文件中的函数

PHP将变量传递给另一个文件中的函数,php,function,variables,Php,Function,Variables,index.php require '../include/FunctionFile.php'; $test = "blah"; myfunction($test); function myfunction($test){ global $test; echo $test; } require '../include/FunctionFile.php'; $test = "blah"; $var=myfunction($test);// assign to vaiable e

index.php

require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
function myfunction($test){
    global $test;
    echo $test;
}
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
function myfunction($test){
    return $test;// use return type here
}
FunctionFile.php

require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
function myfunction($test){
    global $test;
    echo $test;
}
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
function myfunction($test){
    return $test;// use return type here
}

我想将
$test
值传递给myfunction,但看起来它不工作,它什么也不返回,错误日志中什么也不返回。

您的函数需要
返回

index.php

require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
function myfunction($test){
    global $test;
    echo $test;
}
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
function myfunction($test){
    return $test;// use return type here
}
FunctionFile.php

require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
function myfunction($test){
    global $test;
    echo $test;
}
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
function myfunction($test){
    return $test;// use return type here
}

您的函数需要返回值

index.php

require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
function myfunction($test){
    global $test;
    echo $test;
}
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
function myfunction($test){
    return $test;// use return type here
}
FunctionFile.php

require '../include/FunctionFile.php';
$test = "blah";
myfunction($test);
function myfunction($test){
    global $test;
    echo $test;
}
require '../include/FunctionFile.php';
$test = "blah";
$var=myfunction($test);// assign to vaiable
echo $var;
function myfunction($test){
    return $test;// use return type here
}
你也可以试试这个
myfunction(“args”);
函数myfunction($test){
回声试验;
}
您也可以试试这个
myfunction(“args”);
函数myfunction($test){
回声试验;

}
我知道另一位mate已经提供了解决方案,因此我将为未来方面添加我的答案

假设您有两个函数
getHello()
getbye()
,它们的定义不同,用途相同

// 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 
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
结果:

'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 
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
结果:

'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 
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
结果应该是相同的,因为
get再见()
已经
echo'ed
了结果

现在使用您的代码示例:

'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 
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
为什么它在您的代码中不起作用?:

'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 
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"; 

我知道另一位mate已经提供了解决方案,因此我正在为未来方面添加我的答案

假设您有两个函数
getHello()
getbye()
,它们的定义不同,用途相同

// 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 
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
结果:

'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 
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
结果:

'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 
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
结果应该是相同的,因为
get再见()
已经
echo'ed
了结果

现在使用您的代码示例:

'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 
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
为什么它在您的代码中不起作用?:

'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 
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
是类的一部分吗?或者只是简单的书写function@urfusion不,这是一个简单的函数。你确定包含函数的文件正确吗?FunctionFile.php的文件路径正确吗?它在我这边起作用了。@RamRaider是的,我确定你的
myfunction
是类的一部分吗?或者只是简单的书写function@urfusion不,这是一个简单的函数。你确定包含函数的文件正确吗?FunctionFile.php的文件路径正确吗?它在我这边起作用了。@RamRaider是的,我确定用它来检查页面
ini\u集合中的错误('display\u errors',1);ini设置(“显示启动错误”,1);错误报告(E_全部)为之前的回复道歉!这是我的错,它运行良好。。Thanksnice one@Saty:很好的回答使用此选项检查页面
ini\u集合中的错误(“显示错误”,1);ini设置(“显示启动错误”,1);错误报告(E_全部)为之前的回复道歉!这是我的错,它运行良好。。谢谢Saty一号:回答得很好