PHP函数不回显变量

PHP函数不回显变量,php,function,variables,echo,Php,Function,Variables,Echo,我在一个名为config.php的文件中有以下行: $objects["settings"]["template"]["value"] = "yes"; 然后是一个名为funcs.php的文件,其中包含config.php,并包含以下行 echo $objects["settings"]["template"]["value"]; function testfunction() { echo "<br />function output: ".$objects["setti

我在一个名为config.php的文件中有以下行:

$objects["settings"]["template"]["value"] = "yes";
然后是一个名为funcs.php的文件,其中包含config.php,并包含以下行

echo $objects["settings"]["template"]["value"];
function testfunction() {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}
为什么函数的输出不读取变量,而它们是空的

yes
function output:
我期望的输出是:

yes
function output: yes
提前谢谢


编辑:我同意尤达的答案,因为这是关于将$objects作为参数传递的最详细答案。感谢所有回答的人

使用global可以工作,但只有在无法通过函数传递变量时才应该使用,因为它有一些安全风险

$GLOBALS["settings"]["template"]["value"] = 'yes';

这是因为它将变量的范围更改为全局范围

$GLOBALS["settings"]["template"]["value"] = 'yes';

function test(){

    echo $GLOBALS["settings"]["template"]["value"];

}

test();

上面使用global打印出的
yes

可以工作,但是只有当您无法通过函数传递变量时,才应该使用它,因为它有一些安全风险

$GLOBALS["settings"]["template"]["value"] = 'yes';

这是因为它将变量的范围更改为全局范围

$GLOBALS["settings"]["template"]["value"] = 'yes';

function test(){

    echo $GLOBALS["settings"]["template"]["value"];

}

test();

上面使用global打印出的
yes

可以工作,但是只有当您无法通过函数传递变量时,才应该使用它,因为它有一些安全风险

$GLOBALS["settings"]["template"]["value"] = 'yes';

这是因为它将变量的范围更改为全局范围

$GLOBALS["settings"]["template"]["value"] = 'yes';

function test(){

    echo $GLOBALS["settings"]["template"]["value"];

}

test();

上面使用global打印出的
yes

可以工作,但是只有当您无法通过函数传递变量时,才应该使用它,因为它有一些安全风险

$GLOBALS["settings"]["template"]["value"] = 'yes';

这是因为它将变量的范围更改为全局范围

$GLOBALS["settings"]["template"]["value"] = 'yes';

function test(){

    echo $GLOBALS["settings"]["template"]["value"];

}

test();

上面打印出的
yes

只需将对象作为参数传递即可

function testfunction($objects) {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}
function testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; }
只需将对象作为参数传递即可

function testfunction($objects) {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}
function testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; }
只需将对象作为参数传递即可

function testfunction($objects) {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}
function testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; }
只需将对象作为参数传递即可

function testfunction($objects) {
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}
function testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; }
变量不在函数范围内。要使变量在函数中可用,必须将is参数传递到函数中。您还需要修改函数定义。见下面的示例

// function definition
function testFunction($variable) {
    echo $variable;
}

// call the function
$your_variable = "Some output";
testFunction($your_variable);

这将为您提供预期的行为。

该变量不在函数范围内。要使变量在函数中可用,必须将is参数传递到函数中。您还需要修改函数定义。见下面的示例

// function definition
function testFunction($variable) {
    echo $variable;
}

// call the function
$your_variable = "Some output";
testFunction($your_variable);

这将为您提供预期的行为。

该变量不在函数范围内。要使变量在函数中可用,必须将is参数传递到函数中。您还需要修改函数定义。见下面的示例

// function definition
function testFunction($variable) {
    echo $variable;
}

// call the function
$your_variable = "Some output";
testFunction($your_variable);

这将为您提供预期的行为。

该变量不在函数范围内。要使变量在函数中可用,必须将is参数传递到函数中。您还需要修改函数定义。见下面的示例

// function definition
function testFunction($variable) {
    echo $variable;
}

// call the function
$your_variable = "Some output";
testFunction($your_variable);

这将为您提供预期的行为。

该变量不在函数范围内。您可以通过使用
global
关键字来实现这一点。然而,这样做是不好的做法,您应该通过函数参数将值传递给函数

使用
全局
(不良做法):

$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction(){
全球$对象;
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; } testfunction();
通过函数参数传递值:

$objects["settings"]["template"]["value"] = "yes";

function testfunction($objects) {   
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}

testfunction($objects);
$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; } testfunction($objects);
变量不在函数范围内。您可以通过使用
global
关键字来实现这一点。然而,这样做是不好的做法,您应该通过函数参数将值传递给函数

使用
全局
(不良做法):

$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction(){
全球$对象;
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; } testfunction();
通过函数参数传递值:

$objects["settings"]["template"]["value"] = "yes";

function testfunction($objects) {   
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}

testfunction($objects);
$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; } testfunction($objects);
变量不在函数范围内。您可以通过使用
global
关键字来实现这一点。然而,这样做是不好的做法,您应该通过函数参数将值传递给函数

使用
全局
(不良做法):

$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction(){
全球$对象;
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; } testfunction();
通过函数参数传递值:

$objects["settings"]["template"]["value"] = "yes";

function testfunction($objects) {   
    echo "<br />function output: ".$objects["settings"]["template"]["value"];
}

testfunction($objects);
$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction($objects){
echo“
函数输出:“.$objects[“设置”][“模板”][“值”]; } testfunction($objects);
变量不在函数范围内。您可以通过使用
global
关键字来实现这一点。然而,这样做是不好的做法,您应该通过函数参数将值传递给函数

使用
全局
(不良做法):

$objects[“设置”][“模板”][“值”]=“是”;
函数testfunction(){
全球$对象;
欧共体