如果我在函数之前更改全局变量,为什么不起作用?
Why doesn't work if I change variables global, before the function?
接下来的 PHP 代码将名称的首字母大写,其他字母小写:
<?php
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";
fix_names();
echo $a1 . " " . $a2 . " " . $a3;
function fix_names()
{
global $a1;
global $a2;
global $a3;
$a1 = ucfirst(strtolower($a1));
$a2 = ucfirst(strtolower($a2));
$a3 = ucfirst(strtolower($a3));
}
/*Output: William Henry Gates*/
?>
如果我在函数外将变量范围更改为全局,它不起作用:
<?php
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";
global $a1;
global $a2;
global $a3;
fix_names();
echo $a1 . " " . $a2 . " " . $a3;
function fix_names()
{
$a1 = ucfirst(strtolower($a1));
$a2 = ucfirst(strtolower($a2));
$a3 = ucfirst(strtolower($a3));
}
/*Output: WILLIAM henry gatES*/
?>
请解释为什么它不起作用!
Any variable used inside a function is by default limited to the local function scope. For example:
<?php
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variable */
}
test();
?>
This script will not produce any output because the echo statement refers to a local version of the $a
variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable.
In PHP global variables must be declared global inside a function if they are going to be used in that function.
参考:http://php.net/variables.scope.
因此,为了从函数外部访问变量,您需要在函数 global
内部 声明它们,否则您将尝试执行 ucfirst(strtolower($a1))
在一个甚至不存在于当前(本地)作用域中的变量 $a1
上。
接下来的 PHP 代码将名称的首字母大写,其他字母小写:
<?php
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";
fix_names();
echo $a1 . " " . $a2 . " " . $a3;
function fix_names()
{
global $a1;
global $a2;
global $a3;
$a1 = ucfirst(strtolower($a1));
$a2 = ucfirst(strtolower($a2));
$a3 = ucfirst(strtolower($a3));
}
/*Output: William Henry Gates*/
?>
如果我在函数外将变量范围更改为全局,它不起作用:
<?php
$a1 = "WILLIAM";
$a2 = "henry";
$a3 = "gatES";
global $a1;
global $a2;
global $a3;
fix_names();
echo $a1 . " " . $a2 . " " . $a3;
function fix_names()
{
$a1 = ucfirst(strtolower($a1));
$a2 = ucfirst(strtolower($a2));
$a3 = ucfirst(strtolower($a3));
}
/*Output: WILLIAM henry gatES*/
?>
请解释为什么它不起作用!
Any variable used inside a function is by default limited to the local function scope. For example:
<?php
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variable */
}
test();
?>
This script will not produce any output because the echo statement refers to a local version of the
$a
variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable.In PHP global variables must be declared global inside a function if they are going to be used in that function.
参考:http://php.net/variables.scope.
因此,为了从函数外部访问变量,您需要在函数 global
内部 声明它们,否则您将尝试执行 ucfirst(strtolower($a1))
在一个甚至不存在于当前(本地)作用域中的变量 $a1
上。