Is it possible to enclose code fragments in PHP within brackets (without using the fragment as a function)?
Would the following code behave the same way as it would without the curly brackets? Or might there be any problems depending on what kind of code used inside or outside the brackets?
For example, will this:
<?php // First Code-Block {# several lines of code } // Second Code-Block {# another several lines of code } ?>
Always behave the same way as this:
<?php // First Code-Block # several lines of code // Second Code-Block # another several lines of code ?>
Update: One of the goals, as stated in "My1"'s comment as well, is to structure large code sections. Especially since most IDEs give you the option to collapse the lines between the brackets.
Especially in consideration of "dragondreamer"'s "Luke Mills"'s answers I played around with it a bit, and so far I didn't encounter any side effects. Of course, this might change with new PHP Versions in the future but "Luke Mills"'s answer gives good pointers on what to keep an eye on.
Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.
As for me, curly braces serve as a substitution for concatenation, they are quicker to type and code looks cleaner. Remember to use double quotes (" ") as their content is parsed by PHP, because in single quotes (' ') you'll get the literal name of variable provided: <?
It simply means that it is an array. You use the square brackets, with a key (which can be a name (string, wrapped in quotes), or a number) to identify the proper value from the array.
Note: Curly brackets '{ }' are used to destructure the JavaScript Object properties.
Yes, but it won't create a new local scope. It's not something that would normally be done. Usually people mark blocks like this with comments.
Update:
It took a bit of digging to find a reference to it in the manual, but here it is:
http://www.php.net/manual/en/control-structures.intro.php
Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter.
The key here is statements can be grouped into a statement-group by encapsulating a group of statements with curly braces.
I also had a look for a reference to variable scope as it relates to this situation, but the manual doesn't specifically mention it, however you can think of it like this:
In PHP, functions and classes create a variable scope. You can read about that here. But a statement-group (as described above) does not. Don't think of the curly braces of a statement-group like the function (or class) wrapping brackets, but think of them like the curly braces that wrap the statement-group of control structures (if
, for
, while
, switch
, etc.) - because that's exactly what they are. It's clear that if you're using an if
statement (or any other control structure) that the braces don't introduce a new scope, they are simply wrappers for a block of statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With