In PHP are all declared variables global?

7

In C # there is the concept of local variables, see the example below:

if (true) {
    int valor = 10;
}
else {
    valor = 5;
}

Console.Write(valor);

The above code will return an error saying that the valor variable does not exist in the current context:

error CS0103: The name 'valor' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings

That is, the variable valor only exists within if , and you can not access it outside if .

However, in PHP it does not work the same way as C #, see this example:

<?php
if (true) {
    $valor = 10;
}
else {
    $valor = 5;
}

echo 'Valor: ' . $valor;

The output of the above PHP script will be:

  

Value: 10

However, it is possible to access the variable valor , even if it was not declared in the same context or scope, it seems to be in some kind of global scope, and this gave me the following doubts.

Questions

  • All variables declared in PHP are global?
  • Is there any way to declare local variables in PHP?
  • If all variables are of global scope their lifetime is of according to the lifetime of the script?
  • asked by anonymous 26.12.2016 / 17:13

    3 answers

    5
      

    Are all variables declared in PHP global?

    No, there are three types of scopes in PHP that are: conditional, function, and class (attributes, which works a bit differently). PHP by default has a more flexible scope, that is, once you enter the code block, the variable is still accessible. A more classic example is the foreach.

    Conditional definition example:

    if(false){
        $var = 'teste'; 
    }else{
    
    }
    echo $var; //Notice: Undefined variable: var in
    

    The same is for setting functions:

    if(false){
        function condicional(){
            echo 'função condicional chamada';
        }
    }else{
    
    }
    condicional(); //undefined function condicional()
    

    Calling this code returns an error, switch false to true and see what the result is now.

    Flexible scope example

    $arr = range(1,3);
    foreach($arr as $item){
        echo $item .'<br>';
    }
    echo 'item ainda existe => '. $item * 2;
    
      

    Is there any way to declare local variables in PHP?

    Local variables are known to belong to a function, they are not accessible to other functions or part of codes.

    There is no way to make a scope more rigid, one possibility is to make the variable inaccessible using unset() after use.

      

    If all variables are of global scope, their lifetime is according to the lifetime of the script?

    It is not exactly global, once the variable is defined it will only be disabled / deallocated by one of the three scopes, manually and at the end of the script.

        
    26.12.2016 / 17:29
    4

    First, there is a terminology problem there. Global variables in scope and lifetime for every application. Being inside a function indicates that it is local. It's like this in C #, PHP and several languages, but not all.

    C # has a block scope. It is enough to have a block (the ones that are usually composed of keys (unless it's just a line, which can omit the keys), PHP does not have scope, but that does not mean that it is global. / p>

    In general this is not a big problem because functions should be small and it is rare that the lexical scope (this "regional" scope of the blocks) is so useful. I imagine, but I'm not sure, that anonymous functions generate new scope. If you consider that PHP is a script language, the scope of blocks is less useful.

    Variables in PHP are stored in a symbol table, roughly as if everything were in a large associative array, so you can remove the variable at any time, but do not advise doing so . It's just programming in a more or less organized way and you will not have problems.

    Then explicitly replying:

      

    Are all variables declared in PHP global?

    No.

      

    Is there any way to declare local variables in PHP?

    Just declare inside the function and it will be local.

      

    If all variables are of global scope, their lifetime is according to the lifetime of the script?

    The really global ones do.

        
    26.12.2016 / 17:37
    4

    You may get the impression that the variable is global because you can access it (or at least try to access it without causing a fatal error) without having previously set it.

    This subject is very interesting and subject to much reflection.

    One really important thing to pay special attention to is the following: In any programming language, ALL variables are LOCAL at least .

    There are usually no people speaking this way, precisely because there are LOCAL and GLOBAL terminologies. Which suggests that the variable either is of one type or is of another, as if they were antonyms. But analyzing the question coldly, you will see that it is not so, LOCAL is not the opposite of GLOBAL. These names are just characteristics, and a variable because they have both characteristics.

    A GLOBAL variable is nothing more than a LOCAL variable defined in the GLOBAL scope. That is, it is still LOCAL, but with the extra feature of GLOBAL for being part of the GLOBAL scope.

    In PHP, scopes overlap global variables, which is quite curious, but this is due to the dynamic nature of PHP. So there is a SUPERGLOBAL called $ _GLOBALS , which can be used to access the global variables in any scope.

        
    26.12.2016 / 18:14