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.