What is the purpose of { } (keys) to be used to delimit PHP code?

Asked

Viewed 336 times

4

In PHP, you can use the keys in any code snippet, for example, to separate blocks aesthetically.

Example:

{ $a = 1; }

{
    class X{}
}

{
    {
        function x() {}
    }
}

In these examples, none of them an error is generated.

I fully understand the importance of using the parenthesis in some cases. As in this example:

($a + $b) * $c;

$a + ($b * $c);

However, as for the keys, what is the purpose of PHP to allow this?

1 answer

5


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!

  • 1

    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.

  • Exactly @Edilson

  • kkk thanks @rray, sad but true ne? rs do what

  • Read the first sentence and see if you find any errors.

  • 1

    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

  • It’s not a Portuguese error, that’s what you said => bizarre.

  • 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#

  • 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.

Show 4 more comments

Browser other questions tagged

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