According to PHP documentation
Any PHP script is built by a series of instructions. A
instruction can be an assignment, a function call, a loop,
a conditional instruction, or even an instruction that does nothing (a
empty command). Instructions usually end with a semicolon.
In addition, the instructions can be grouped into a group of commands
by encapsulation of a group of commands with keys. A group
commands is an instruction as well. The various types of instructions are
described in this chapter.
Although very poorly written or translated, it means that it is possible to group command blocks, for example when using a if
or a for
, these instructions will execute the next command or command block.
// Executando o próximo comando.
if ($foo == $bar)
echo 'comando a ser executado';
// Executando o próximo bloco de comando.
if ($foo == $bar) {
echo 'inicio do bloco de comando';
echo 'meio do bloco de comando';
echo 'final do bloco de comando';
}
Another way to use keys is to use variables in the middle of strings started with double quotes.
$str = "Uma string contendo a váriavel {$teste}";
$str = "Uma string apresentando um atributo {$this->teste}";
$str = "Uma string apresentando uma posição de um atributo {$this->teste[1]}";
Keys do not start a new code scope, only group a group of commands.
Another application for this very common in C# known as Regions is to group codes for documentation, but honestly I think only pollute the code.
// Comentando o bloco seguinte de ações
{
echo 'imprime alguma coisa';
$a = 2;
$b = 3;
$c = 4;
$x = $a + $b * $c;
echo $x;
}
With the above grouping of code, in some editors you can contract the code by omitting it.
+1 by "very badly written or translated", pq is even!
– rray
For the simple fact that PHP allow to structure the code in the most diverse possible ways, presenting compatibility with other languages at the semantic level, it can be stated that serve only to help in reading, and locate code chains with the same purpose more easily.
– Edilson
Exactly @Edilson
– marcusagm
kkk thanks @rray, sad but true ne? rs do what
– marcusagm
Read the first sentence and see if you find any errors.
– rray
I didn’t look at Portuguese error, but what do you mean methods and variables? is it referring to mandatory attributes or parameters of methods? kkk looks like class attributes, and as far as I know they cannot be specified in interfaces, only constants o.O
– marcusagm
It’s not a Portuguese error, that’s what you said => bizarre.
– rray
In fact, it is understood that PHP has many useless features. I have never seen anyone using it, not even the documentation explains it (direct). I saw this in a code from a programmer who previously programmed in C#
– Wallace Maxters
I think the following, if you need to create changes, can very well refactoring and transform into methods or functions, so you improve readability and still reuse code.
– marcusagm