为什么不使用新变量显示输出?
Why the output is not displayed with a new variable?
为什么不显示输出($h)?
我不想删除(函数)
请更正。
这是我的代码:
<?php
$x = 5 . "*";
$y = 10 . "=";
function myTest() {
global $x, $y;
$h = $x + $y; //new variable
}
myTest(); // run function
echo $x . $y . $h; // output the value
?>
$h
仅在 test
函数内有作用域。要获得 $h
值,您可以将 $h
设为全局值,或者最好将 return 设为 myTest 函数中的值。
注意:函数通常应该是一个独立的单元。通过在函数内提供全局作用域(使用 global $x, $y
),向代码引入了不需要的且可能有害的依赖项。下面的代码是一个示例,只是为了说明您问题的解决方案。
function myTest() {
global $x, $y;
return $x + $y;
}
$h = myTest();
你应该使用变量 $h
作为全局变量
$x = 5 . "*";
$y = 10 . "=";
$h = null;// declare $h
function myTest() {
global $x, $y, $h; //use $h here
$h = $x + $y; //new variable
}
myTest(); // run function
echo $x . $y . $h; // output the value
为什么不显示输出($h)? 我不想删除(函数)
请更正。
这是我的代码:
<?php
$x = 5 . "*";
$y = 10 . "=";
function myTest() {
global $x, $y;
$h = $x + $y; //new variable
}
myTest(); // run function
echo $x . $y . $h; // output the value
?>
$h
仅在 test
函数内有作用域。要获得 $h
值,您可以将 $h
设为全局值,或者最好将 return 设为 myTest 函数中的值。
注意:函数通常应该是一个独立的单元。通过在函数内提供全局作用域(使用 global $x, $y
),向代码引入了不需要的且可能有害的依赖项。下面的代码是一个示例,只是为了说明您问题的解决方案。
function myTest() {
global $x, $y;
return $x + $y;
}
$h = myTest();
你应该使用变量 $h
作为全局变量
$x = 5 . "*";
$y = 10 . "=";
$h = null;// declare $h
function myTest() {
global $x, $y, $h; //use $h here
$h = $x + $y; //new variable
}
myTest(); // run function
echo $x . $y . $h; // output the value