2015-09-01 00:11 阅读 81


I have this code:

  function ha(){
     return $user;

echo ha();

echo $user; /* GLOBAL VARIABLE VALUE */

So how to define in function local variable that will be accessed throught inside function ha()? So the outout from the echo ha(); will be value $user that is stored in config.php, and last line echo $user needs to be empty on echo...when i define in function static $user i get empty value...so how to define in php static value of variable that is read from config.php and access value only in function?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dongzhong2018 dongzhong2018 2015-09-01 00:13
    function ha(){
        global $user;
        $user2 = 'abc'; //no prefix whatsoever
        echo $user; //prints global $user
        echo $user2; //prints local $user2

    how to define in php static value of variable that is read from config.php and access value only in function?

    It's all about variable scope , if you want a variable to be defined in config file and only be readable from within a function, then your current approach is incorrect. Anything declared in main scope (such as config file loaded in main scope) will be accesible 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().

    点赞 评论 复制链接分享
  • doudoulb1234 doudoulb1234 2015-09-01 00:25

    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 in the PHP docs.

    点赞 评论 复制链接分享
  • duanjing2013 duanjing2013 2015-09-01 01:02
      function ha(){
         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.

    点赞 评论 复制链接分享
  • dongsonglian7303 dongsonglian7303 2019-02-12 12:37

    You are asking about defining 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 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 (e.g. inside a config.php), then you need to initialize it using a value of dedsired type. e.g.: $user = ''.

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

    You wanted to:
    Define 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;
      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 value of $user. This is needed, because if you call the function second time, 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 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 in function static $user i get 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.

    点赞 评论 复制链接分享