What is the purpose of individual curly braces in PHP

Is this a parser error, or is there any usefulness in using curly braces like this?

$x = 1;

{
    $x++;
}

As far as I can see, it behaves exactly as if the brackets were not there, so why is this a valid syntax?

+4
source share
5 answers

Its purpose is to group operations for use in places where only one operation is permitted.

if (cond) foo();equals if (cond) { foo(); }
foo();equal{ foo(); }

+2
source

There is no hidden meaning. This is more readability.

Check manual :

PHP script . , , , , ( ). . , . - .

+7

, , , .

  • , ( ), .
  • .
+2
source
{
$x++;
}

just like

{$x++;}

which looks like

$x++;

They should indicate a code block.

You can also consider this:

if (true) {
  $x++;
}

The code has valid syntax, and it will behave exactly without the if condition. You can reload your code with "valid" and useless code as much as you want.

0
source

The copied brackets can be used to change the scope in the local scope. Therefore, if you define a variable in brackets, it will not be available outside the brackets

-2
source

Source: https://habr.com/ru/post/1607794/


All Articles