In PHP are all variables declared global?

Asked

Viewed 815 times

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 returns an error saying that the variable valor 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 exists only within the if, and cannot access it outside the if.

However, in PHP it doesn’t work the same as C#, see this example:

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

echo 'Valor: ' . $valor;

The above PHP script output will be:

Value: 10

However, it is possible to access the variable valor, even though it has not been stated in the same context or scope, it seems to be in some kind of global scope, and this has raised the following doubts.

Doubts

  1. All variables declared in PHP are global?
  2. Is there any way to declare local variables in PHP?
  3. If all variables are global in scope, their lifespan is according to the script lifetime?
  • 2

    http://php.net/manual/en/language.variables.scope.php. In the PHP documentation it is well explained.

  • @Rafaelmafra would be interesting to have an answer here also after all I think that this is the purpose of Sopt, I read the documentation, but I did not understand many things I was even more confused because it treats as if everything were local.

3 answers

7


All variables declared in PHP are global?

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

Example of conditional definition:

if(false){
    $var = 'teste'; 
}else{

}
echo $var; //Notice: Undefined variable: var in

The same goes for defining functions:

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

}
condicional(); //undefined function condicional()

The call of this code returns an error, change the false for true and see what the result is now.

Example of flexible scope

$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 global in scope their lifespan is according to the lifespan of the script?

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

5

First, there’s a terminology problem there. Global variables have life span and time throughout the application. Being inside a function indicates that it is local. That’s how it is in C#, PHP and several languages, but not all.

C# has a block scope. Just have a block (those that are usually composed of keys (unless it’s just a line, which can omit keys). PHP does not have, the scope is function. But that does not mean that it is global.

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 am not sure, that anonymous functions generate new scope. If you consider that PHP is a language of script, the scope of blocks should be even less useful.

PHP variables are stored in a symbol table, roughly it’s like everything is in a big array associative, so it is possible to remove the variable at any time, but I do not advise doing this. Just program in a more or less organized way and you won’t have any problems.

Then explicitly responding:

All variables declared in PHP are global?

Not.

Is there any way to declare local variables in PHP?

Just declare within the function and it will be local.

If all variables are global in scope their lifespan is according to the lifespan of the script?

The really global ones yes.

4

You may get the impression that the variable is global due to the fact that you can access it (or at least try to access it without it resulting in a fatal error) without having previously defined it.

This subject is very interesting and subject to much reflection.

One really important thing to pay special attention to is this:: In any programming language, EVERY variable is at least LOCAL.

Normally there are no people saying it that way, precisely because there are LOCAL and GLOBAL terminologies. Which suggests that the variable is either of one type or of another, as if they were antonyms. But if you look coldly at the question, you will see that it is not quite so, LOCAL is not the opposite of GLOBAL. These names are only characteristics, and a variable for having the two 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 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. That’s why there’s a SUPERGLOBAL called $_GLOBALS, which can be used to access global variables in any scope.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.