简体   繁体   中英

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()?

So the output from the 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?

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?

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() .

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 .

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. Any variable declared in a function not explicitly set to be $global will not be 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. 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. eg: $user = '' .

  • import an external config.php file inside a local scope of a function

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 must not be visible from outside of that function.

You need to put the require_once statement inside that function.

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

Using static keyword will let the function keep the value of $user. This is needed, because if you call the function a second time, file config.php will not be included again ( require_once() ).

PHP has three variable scopes, global, local, and static. Static is not the same as local , but it behaves the same in a sense to where the variable is accessible. Static scope in a function is a PHP specialty, read about it .

when I define $user in function static I get an empty value..

You do, because when you used require_once() outside of the function, $user variable was defined in global scope. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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