php variable scope

后端 未结 6 1993
栀梦
栀梦 2020-12-01 19:31

i\'m confused about the php variable scope. such as:

while(true){
    $var = \"yes , it is a test!\";
  }
  printf($var)

the $var

相关标签:
6条回答
  • 2020-12-01 19:58

    If you do while(true) you will not get out of the while, so it wouldn't matter. But if you would have a real expression, something like this (this is a useless example, i know)

    $i=0
    while($i<10){ 
       $var = "yes , it is a test!"; 
       $i++;
     } 
     printf($var);
    

    Will just work. There is no special "while" variable scope, the printf will print your string. check : http://php.net/manual/en/language.variables.scope.php

    0 讨论(0)
  • 2020-12-01 19:58

    Notice: Different PHP-version behave differently on $GLOBALS, $_SERVER ($HTTP_SERVER_VARS [deprecated]) and $_SESSION. Please check that. Old PHP page are not always upwards compatible. You should check that.

    0 讨论(0)
  • 2020-12-01 20:05

    For the complete answer check the documentation:

    http://php.net/manual/en/language.variables.scope.php

    0 讨论(0)
  • 2020-12-01 20:13

    In PHP, a while loop doesn't create a new scope. So it will be available in the function

    0 讨论(0)
  • 2020-12-01 20:13

    Loop does not have any scope in PHP. variable is simply available outside the loop.

    just echo outside the loop;

    echo $var;

    0 讨论(0)
  • 2020-12-01 20:21

    while is not a function. scope of variable refers to variable inside functions and classes

    0 讨论(0)
提交回复
热议问题