Curly Braces Notation in PHP

Asked
Active3 hr before
Viewed126 times

8 Answers

bracescurlynotation
90%

Curly braces are used to denote string or variable interpolation in PHP. It allows you to create 'variable functions', which can allow you to call a function without explicitly knowing what it actually is.,Curly braces are used to explicitly specify the end of a variable name.,It's also used in strings to more easily identify interpolation, if you want to:,Connect and share knowledge within a single location that is structured and easy to search.

Using this, you can create a property on an object almost like you would an array:

$property_name = 'foo';
$object - > {
   $property_name
} = 'bar';
// same as $object->foo = 'bar';

Or you can call one of a set of methods, if you have some sort of REST API class:

$allowed_methods = ('get', 'post', 'put', 'delete');
$method = strtolower($_SERVER['REQUEST_METHOD']); // eg, 'POST'

if (in_array($method, $allowed_methods)) {
   return $this - > {
      $method
   }();
   // return $this->post();
}

It's also used in strings to more easily identify interpolation, if you want to:

$hello = 'Hello';
$result = "{$hello} world";
load more v
88%

PHP allows both square brackets and curly braces to be used interchangeably for accessing array elements and string offsets. For example: ,How frequently is the curly brace syntax used?, Deprecate curly brace syntax for accessing array elements and string offsets. ,Wasn't the curly brace syntax deprecated once before?

PHP allows both square brackets and curly braces to be used interchangeably for accessing array elements and string offsets. For example:

$array = [1, 2];
echo $array[1]; // prints 2
echo $array {
   1
}; // also prints 2

$string = "foo";
echo $string[0]; // prints "f"
echo $string {
   0
}; // also prints "f"

Apart from two short notes in the PHP Manual, the curly brace syntax is virtually undocumented. Furthermore, it has reduced functionality compared to the normal bracket syntax. For example, it cannot be used for pushing an element into an array:

$array[] = 3;
echo $array[2]; // prints 3

$array {} = 3; // Parse error: syntax error, unexpected '}'

Nor can it be used to create an array:

$array = [1, 2]; // works

$array = {
   1,
   2
}; // Parse error: syntax error, unexpected '{'

It can't be used for list assignment, either:

[$one, $two] = $array; // works

{
   $one,
   $two
} = $array; // Parse error: syntax error, unexpected ','
load more v
72%

I'd just come across a very weird bit of php code:,Curly braces as of PHP 7.4 are deprecated for accessing arrays. ,https://wiki.php.net/rfc/deprecate_curly_braces_array_access,Array and string offset access using curly braces is officially deprecated from PHP7.4 Ref: https://www.php.net/manual/en/migration74.deprecated.php

I'd just come across a very weird bit of php code:

$oink {
   'pig'
} = 1;
var_dump($oink);

$oink {
   'pig'
} = '123123';
echo $oink {
   'pig'
}; /* => 123123 */
echo $oink['pig']; /* => 123123 */
65%

Usage of opening and closing curly braces is mandatory in all cases where they can be used according to PHP syntax (except case statements).,The closing curly brace must start on a new line and be indented to the same level as the construct with the opening brace. Example:,Coding: Best practices Accessing the database Singletons Static methods Localization Unit tests Handling deprecation Namespaces and class names of user files ,The use of the newest PHP features is strongly recommended for extensions and mandatory for the TYPO3 core.

$goodName
$anotherGoodName
load more v
75%

PHPUnit assertTrue/assertFalse should be simplified to the corresponding dedicated assertion Code Smell,Files should not have too many lines of code Code Smell,Using command line arguments is security-sensitive Security Hotspot,Classes should not be coupled to too many other classes (Single Responsibility Principle) Code Smell

Noncompliant Code Example

if (condition) {
   doSomething();
}
load more v
40%

This mentioned on PHP docs. Also i found small discussion here: PHP curly braces in array notation.,PHP-FIG does not contains any recommendations about which way is preferable. ,Note that this solution will not work on Mountain Lion (10.8).,"Square bracket notation" for array elements would be more unified and acceptable."Curly bracket notation" will work in expressions but not in variable interpolation when dealing with accessing of an array element.Consider the following example:

"Square bracket notation" for array elements would be more unified and acceptable.
"Curly bracket notation" will work in expressions but not in variable interpolation when dealing with accessing of an array element.
Consider the following example:

$myArray = [1, 2];
$index = 1;

echo "value at index $index is $myArray[$index]"; // outputs "value at index 1 is 2"

echo "value at index $index is $myArray{$index}"; // will throw "Notice: Array to string conversion"

var_dump($myArray {
   $index
}); // outputs "int(2)"
load more v
22%

Let 's say you have something like this in your code:

$str = "test";
echo($str {
   0
});

since PHP 7.4 curly braces method to get individual characters inside a string has been deprecated, so change the above syntax into this:

   $str = "test";
echo($str[0]);
60%

I couldn't find any response related to those double curly braces. Only find for wp.customize javascript notation or angular.js but none of them explains this situation.,Asking for help, clarification, or responding to other answers.,Database Administrators,Please be sure to answer the question. Provide details and share your research!

In your example {{image-about}} tells WordPress to use the image defined in ['attachments']['image-about'] as the thumbnail for those posts in the 'starter content'. This functionality is specific to the "Starter Content" feature added in WordPress 4.7, which is described here, and not a PHP language feature.

{
   {
      image - about
   }
}
load more v

Other "braces-curly" queries related to "Curly Braces Notation in PHP"