如何在 PHP 中的函数中定义局部变量?
How can I define a local variable in a function in PHP?
我有这个代码:
require_once('../config.php');
function ha(){
global $user; /* I need this to define local-only access from this function */
return $user;
}
echo ha();
echo $user; /* Global variable value */
那么我如何在函数中定义一个将通过内部函数 ha() 访问的局部变量?
因此 echo ha();
的输出将是存储在文件 config.php 中的值 $user,最后一行 echo $user
在 echo 上需要为空...当我在静态函数中定义 $user 时,我得到一个空值...那么如何在 PHP 中定义从文件 [=20] 读取的变量的静态值=]config.php 并且只访问函数中的值?
function ha(){
global $user;
$user2 = 'abc'; // No prefix whatsoever
echo $user; // Prints global $user
echo $user2; // Prints local $user2
}
how can I define a static value of variable in PHP that is read from file config.php and only access the value in the function?
一切都是为了 variable scope。如果你想让一个变量定义在配置文件中,并且只能在函数中读取,那么你现在的做法是不正确的。
在主作用域中声明的任何内容(例如在主作用域中加载的配置文件)都可以从代码中的几乎任何位置访问。如果您不希望通过 ha()
以外的方式访问变量,则需要在 ha()
.
中定义它们
假设我理解你在这里的要求,你可以这样做:
class Private {
private $user = 'value'; // Declare $user as a private variable
function ha() {
echo $this->user;
}
}
$object = new Private();
echo $object->user; // Returns fatal error
$object->ha(); // Echoes 'value'
有关可见性的更多信息,请参见 PHP documentation。
require_once('../config.php');
function ha(){
$user; /* THIS I NEED TO DEFINE LOCAL ONLY ACCESS FROM THIS FUNCTION */
return $user;
}
echo ha();
//echo $user; /* (unnecessary) GLOBAL VARIABLE VALUE, same as echoing the result of the function!*/
请参阅 variable scope 上的文档。第二个 echo $user
现在不需要了。在未明确设置为 $global
的函数中声明的任何变量都不是全局变量。
您问的是定义一个局部变量,但这本身并不能解决您的问题。
为了那些通过搜索来到这里的人,我会回答这两个问题。
- 如何在函数中定义局部变量:
PHP 在第一次使用时定义一个变量。没有用于声明局部作用域的关键字。默认情况下,函数内的所有变量都是局部变量(即使是与另一个全局变量同名的变量)。
'A first use'表示赋值,而不是使用return中的变量或条件。如果您不确定您在 return 上使用的变量是否会被分配一个值(例如在 config.php 文件中),那么您需要使用所需类型的值。例如:$user = ''
.
- 在函数的本地范围内导入外部 config.php 文件
您想:
在函数内定义一个局部变量,只能在该函数的范围内访问。这个局部变量 $user 被分配了一个来自 config.php 文件的值。
$user 不能从该函数的外部可见。
您需要将 require_once
语句放入该函数中。
If the include occurs inside a function within the calling file, then
all of the code contained in the called file will behave as though it
had been defined inside that function.
http://php.net/manual/en/function.include.php
function ha(){
static $user;
require_once('../config.php');
if (!isset($user)) { $user = ''; } // Only needed if you are not sure config.php will define the $user variable
return $user;
}
echo ha();
echo $user; // Will output nothing
使用static
关键字会让函数保留$user的值。这是必需的,因为如果您第二次调用该函数,将不会再次包含文件 config.php (require_once()
).
PHP具有三种变量作用域,全局、局部和静态。 static 与 local 不同,但在某种意义上,它的行为与变量可访问的位置相同。函数中的静态作用域是 PHP 的专长,read about it.
when I define $user in function static I get an empty value..
确实如此,因为当您在函数外部使用 require_once()
时,$user 变量是在全局范围内定义的。然后,您通过使用 $user 在 ha 函数中定义了 another $user 变量,而没有通过 global $user
将其声明为全局变量,如你后来做了。
我有这个代码:
require_once('../config.php');
function ha(){
global $user; /* I need this to define local-only access from this function */
return $user;
}
echo ha();
echo $user; /* Global variable value */
那么我如何在函数中定义一个将通过内部函数 ha() 访问的局部变量?
因此 echo ha();
的输出将是存储在文件 config.php 中的值 $user,最后一行 echo $user
在 echo 上需要为空...当我在静态函数中定义 $user 时,我得到一个空值...那么如何在 PHP 中定义从文件 [=20] 读取的变量的静态值=]config.php 并且只访问函数中的值?
function ha(){
global $user;
$user2 = 'abc'; // No prefix whatsoever
echo $user; // Prints global $user
echo $user2; // Prints local $user2
}
how can I define a static value of variable in PHP that is read from file config.php and only access the value in the function?
一切都是为了 variable scope。如果你想让一个变量定义在配置文件中,并且只能在函数中读取,那么你现在的做法是不正确的。
在主作用域中声明的任何内容(例如在主作用域中加载的配置文件)都可以从代码中的几乎任何位置访问。如果您不希望通过 ha()
以外的方式访问变量,则需要在 ha()
.
假设我理解你在这里的要求,你可以这样做:
class Private {
private $user = 'value'; // Declare $user as a private variable
function ha() {
echo $this->user;
}
}
$object = new Private();
echo $object->user; // Returns fatal error
$object->ha(); // Echoes 'value'
有关可见性的更多信息,请参见 PHP documentation。
require_once('../config.php');
function ha(){
$user; /* THIS I NEED TO DEFINE LOCAL ONLY ACCESS FROM THIS FUNCTION */
return $user;
}
echo ha();
//echo $user; /* (unnecessary) GLOBAL VARIABLE VALUE, same as echoing the result of the function!*/
请参阅 variable scope 上的文档。第二个 echo $user
现在不需要了。在未明确设置为 $global
的函数中声明的任何变量都不是全局变量。
您问的是定义一个局部变量,但这本身并不能解决您的问题。
为了那些通过搜索来到这里的人,我会回答这两个问题。
- 如何在函数中定义局部变量:
PHP 在第一次使用时定义一个变量。没有用于声明局部作用域的关键字。默认情况下,函数内的所有变量都是局部变量(即使是与另一个全局变量同名的变量)。
'A first use'表示赋值,而不是使用return中的变量或条件。如果您不确定您在 return 上使用的变量是否会被分配一个值(例如在 config.php 文件中),那么您需要使用所需类型的值。例如:$user = ''
.
- 在函数的本地范围内导入外部 config.php 文件
您想:
在函数内定义一个局部变量,只能在该函数的范围内访问。这个局部变量 $user 被分配了一个来自 config.php 文件的值。
$user 不能从该函数的外部可见。
您需要将 require_once
语句放入该函数中。
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. http://php.net/manual/en/function.include.php
function ha(){
static $user;
require_once('../config.php');
if (!isset($user)) { $user = ''; } // Only needed if you are not sure config.php will define the $user variable
return $user;
}
echo ha();
echo $user; // Will output nothing
使用static
关键字会让函数保留$user的值。这是必需的,因为如果您第二次调用该函数,将不会再次包含文件 config.php (require_once()
).
PHP具有三种变量作用域,全局、局部和静态。 static 与 local 不同,但在某种意义上,它的行为与变量可访问的位置相同。函数中的静态作用域是 PHP 的专长,read about it.
when I define $user in function static I get an empty value..
确实如此,因为当您在函数外部使用 require_once()
时,$user 变量是在全局范围内定义的。然后,您通过使用 $user 在 ha 函数中定义了 another $user 变量,而没有通过 global $user
将其声明为全局变量,如你后来做了。