简体   繁体   English

如何在 PHP 的函数中定义局部变量?

[英]How can I define a local variable in a function in PHP?

I have this code:我有这个代码:

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 */

So how can I define a local variable in the function that will be accessed through inside function ha()?那么如何在函数中定义一个局部变量,该变量将通过内部函数 ha() 访问?

So the output from the echo ha();所以echo ha();的输出will be the value $user that is stored in file config.php , and the last line echo $user needs to be empty on echo...when I define $user in function static I get an empty value...so 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?将是存储在文件config.php 中的值 $user ,最后一行echo $user需要在 echo 上为空...当我在函数 static 中定义 $user 时,我得到一个空值...那么怎么能我在 PHP 中定义了一个从文件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?如何在 PHP 中定义从文件config.php读取的变量的静态值,并且只访问函数中的值?

It's all about variable scope .这都是关于变量范围的 If you want a variable to be defined in the configuration file and only be readable from within a function, then your current approach is incorrect.如果您希望在配置文件中定义一个变量并且只能从函数内部读取,那么您当前的方法是不正确的。

Anything declared in the main scope (such as the configuration file loaded in the main scope) will be accessible from nearly anywhere in the code.在主作用域中声明的任何内容(例如在主作用域中加载的配置文件)都可以从代码中的几乎任何位置访问。 If you don't want variables to be accessed otherwise than through ha() then they need to be defined within ha() .如果您不想通过ha()以外的方式访问变量,则需要在ha()定义它们。

Assuming I understand what you're asking for here, you could do this:假设我明白你在这里要求什么,你可以这样做:

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'

More on visibility is in the PHP documentation .有关可见性的更多信息,请参见PHP 文档

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!*/

Please see the documentation on variable scope .请参阅有关变量范围的文档。 The second echo $user is now unnecessary.现在不需要第二个echo $user Any variable declared in a function not explicitly set to be $global will not be global.在函数中声明的任何未显式设置为$global变量都不是全局变量。

You are asking about defining a local variable, but that by itself would not solve your problem.您正在询问定义局部变量,但这本身并不能解决您的问题。
For the sake of those getting here by a search, I'll answer both.为了那些通过搜索到达这里的人,我会回答两者。

  • How to define a local variable in a function:如何在函数中定义局部变量:

PHP defines a variable on its first use. PHP 在第一次使用时定义了一个变量。 There is no keyword for declaring a local scope.没有用于声明局部作用域的关键字。 All variables inside functions are local by default (even a variable with the same name as another global variable).默认情况下,函数内的所有变量都是局部变量(即使是与另一个全局变量同名的变量)。

'A first use' means assigning a value, not using the variable in return or a condition. “第一次使用”意味着赋值,而不是使用变量作为回报或条件。 If you are not certain that the variable you use on return will be assigned a value (eg inside a config.php file), then you need to initialize it using a value of desired type.如果您不确定返回时使用的变量是否会被赋值(例如在config.php文件中),那么您需要使用所需类型的值对其进行初始化。 eg: $user = '' .例如: $user = ''

  • import an external config.php file inside a local scope of a function在函数的本地范围内导入外部 config.php 文件

You wanted to:你想:

Define a local variable inside a function, accessible only inside that function's scope.在函数内定义一个局部变量,只能在该函数的作用域内访问。 This local variable $user is assigned a value from the config.php file.这个局部变量 $user 被分配了一个来自config.php文件的值。
$user must not be visible from outside of that function. $user 不能从该函数外部可见。

You need to put the require_once statement inside that function.您需要将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 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

Using static keyword will let the function keep the value of $user.使用static关键字将使函数保持 $user 的值。 This is needed, because if you call the function a second time, file config.php will not be included again ( require_once() ).这是必需的,因为如果您第二次调用该函数,将不会再次包含文件config.php ( require_once() )。

PHP has three variable scopes, global, local, and static. PHP 具有三个变量作用域,全局、局部和静态。 Static is not the same as local , but it behaves the same in a sense to where the variable is accessible. Static 与local 不同,但从某种意义上说,它的行为与变量可访问的位置相同。 Static scope in a function is a PHP specialty, read about it .函数中的静态作用域是 PHP 的专长,请阅读

when I define $user in function static I get an empty value..当我在函数 static 中定义 $user 时,我得到一个空值..

You do, because when you used require_once() outside of the function, $user variable was defined in global scope.你这样做,因为当你在函数之外使用require_once() , $user 变量是在全局范围内定义的。 Then you defined another $user variable inside the ha function by using $user, without declaring it to be global by global $user , as you did later.然后,您使用 $user 在ha函数中定义了另一个$user 变量,而没有像稍后那样通过global $user将其声明为全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM