When to Wrap Curly Braces Around a Variable

When to wrap curly braces around a variable

What are PHP curly braces:

You know that a string can be specified in four different ways. Two of these ways are – double quote("") and heredoc syntax. You can define a variable in those 2 types of strings and PHP interpreter will parse or interpret that variable too, within the strings.

Now, there are two ways you can define a variable in a string – simple syntax which is the most used method of defining variables inside a string and complex syntax which uses curly braces to define variables.

Curly braces syntax:

To use a variable with curly braces is very easy. Just wrap the variable with { and } like:

{$variable_name}

Note: There must not be any gap between { and $. Else, PHP interpreter won't consider the string after $ as a variable.

Curly braces example:

<?php
$lang = "PHP";
echo "You are learning to use curly braces in {$lang}.";
?>

Output:

You are learning to use curly braces in PHP.

When to use curly braces:

When you are defining a variable inside a string, PHP might mix up the variable with other characters if using simple syntax to define a variable and this will produce an error. See the example below:

<?php
$var = "way";
echo "Two $vars to defining variable in a string.";
?>

Output:

Notice: Undefined variable: vars …

In the above example, PHP's interpreter considers $vars a variable, but, the variable is $var. To separate a variable name and the other characters inside a string, you can use curly braces. Now, see the above example using curly braces-

<?php
$var = "way";
echo "Two {$var}s to define a variable in a string.";
?>

Output:

Two ways to define a variable in a string.

Source: http://schoolsofweb.com/php-curly-braces-how-and-when-to-use-it/

What do curly braces around a variable in a function parameter mean

This is destructuring assignment syntax.

As another example, the following two lines of code are equal:

const { items } = args

const items = args.items

Simply put, it is a simplified way of accessing specific field of a given variable for further use in that scope.

In your original example, it is declaring a variable items for use in the function body that is the items field of that first argument.

const SortableList = SortableContainer(({items}) => {
// do stuff with items here

is equal to

const SortableList = SortableContainer((input) => {
const items = input.items
// do stuff with items here

Why do I need to wrap array variable with curly brackets in PHP sometimes

It's because you are echoing an array entry inside a double quoted string.

echo "hello $another_variable"; //works if $another_variable can be converted to string
echo "hello $arr['something']"; //error anyway
echo "hello {$arr['something']}"; //works

Basically, if you need to do something more complex than just using a simple $variable, you have to either use:

  • The curly brackets syntax: echo "my {$variable['foobar']}";
  • Concatenation: echo "my ".$variable['foobar'];

When do we need curly braces around shell variables?

In this particular example, it makes no difference. However, the {} in ${} are useful if you want to expand the variable foo in the string

"${foo}bar"

since "$foobar" would instead expand the variable identified by foobar.

Curly braces are also unconditionally required when:

  • expanding array elements, as in ${array[42]}
  • using parameter expansion operations, as in ${filename%.*} (remove extension)
  • expanding positional parameters beyond 9: "$8 $9 ${10} ${11}"

Doing this everywhere, instead of just in potentially ambiguous cases, can be considered good programming practice. This is both for consistency and to avoid surprises like $foo_$bar.jpg, where it's not visually obvious that the underscore becomes part of the variable name.

What do {curly braces} around javascript variable name mean

This is what's known as a destructuring assignment, and it's a new feature of JavaScript 1.7 (and ECMAScript 6) (Currently, only available as part of the Firefox JavaScript engine.) Roughly, it would translate into this:

var ActionButton = require("sdk/ui/button/action").ActionButton;

It seems silly in this example, as there's only one item being assigned. However, you'd be able to use this pattern to assign multiple variables at once:

{x, y} = foo;

Is the equivalent to:

x = foo.x;
y = foo.y;

This can also be used for arrays. For example, you could easily swap two values without using a temporary variable:

var a = 1;
var b = 3;

[a, b] = [b, a];

Browser support can be tracked using kangax' ES6 compatibility table.

javascript: meaning of curly brace between variable name

Essentially curly braces like you've mentioned are Objects in javascript.

So making something like this in javascript

const user = {
name: 'bob',
age: 23,
};

Is making a user Object which you can use like this: user.name which will return bob.

With ES6 you're capable of making Objects from other Objects.

const {user} = state.params;
//user will be state.params.user

The above is basically pulling the property user from the object state.params into a new variable. Really all they're doing is making it so you don't have to write state.params.user each time and rather you can write user.

There's some other cool stuff you can do with this above technique. You can 'merge' arrays and objects into new constants which is pretty cool.

const test = {
...user,
anotherProperty: 'value',
};

With react and redux (if you're using it) you'll see a lot of this: Object.assign({}, state, {}); which is used to create a new object with the previous properties of the state overwritten with the new state (because redux requires a new object). This is kind of the same as using {...state, ...newState}.

Please someone remind me what this ...Object method is called?

This line const isInfo = state.params.mode === 'info'; is a shorthand for declaring a bool. isInfo will be either true or false depending on whether state.params.mode === 'info'.

To translate the above into C++ for you

if (state.params.mode === 'info') {
bool isInfo = true;
else {
bool isInfo = false;
}

I can't remember if there is a similar Object array in C++ as in JavaScript bit think of Objects in JavaScript as Arrays with defined keys. That way the above {...state, ...newState} is like an 'override' of keys. So

int y = [1,2,3];
int x = [3,2,1];

for (i=0;i<=2;i++) {
y[i] = x[i];
}

Something like that I think?

Use of Curly Brackets (Braces) Around a variable C++

It could be written even simpler

int small[26]= {}, large[26]={}, i;

The curly brackets means an initializer list in this case of arrays.

Let assume for example that you want to define an array with elements 1, 2, 3, 4, 5.

You could write

int a[5];

a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;

However C++ allows to assign elements of an array when it is defined. The equivalent record will look

int a[5] = { 1, 2, 3, 4, 5 };

If there are initializers less than the size of the array then remaining elements will be initialized by zeroes. For example

int a[5] = { 1, 2 };

In this case a[0] will be equal tp 1 a[1] will be equal to 2 and all other elements will be equal to 0.

You may omit the size of an array. For example

int a[] = { 1, 2, 3, 4, 5 };

In this case the compiler will allocate as many elements of the array as there are initializers in the initializer list.

Record (valid only in C++. In C it is not allowed)

int a[5] = {};

is equivalent to

int a[5] = { 0 };

that is all elements of the array will be initialized by 0. In the last record the first element is initialized explicitly by zero and all other elements are also initialized by zero because their initializers in the initializer list were not specified.

The same way you can initialize also scalar objects. For example

int x = { 10 };

The only difference that for scalar objects you can specify only one initializer. You even may write without the assignment operator

int x { 10 };

You also can write

int x {};

In this case x will be initialized by 0.

Hotkey for curly braces around variable in Visual Studio Code

I think the fastest way and what I usually do is to press this set of keys:

⌘ + D + {

By the way here are all the shortcuts for macOS:

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf

And here the Windows ones

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf

hope it helps :)

When should I use curly braces for ES6 import?

This is a default import:

// B.js
import A from './A'

It only works if A has the default export:

// A.js
export default 42

In this case it doesn’t matter what name you assign to it when importing:

// B.js
import A from './A'
import MyA from './A'
import Something from './A'

Because it will always resolve to whatever is the default export of A.


This is a named import called A:

import { A } from './A'

It only works if A contains a named export called A:

export const A = 42

In this case the name matters because you’re importing a specific thing by its export name:

// B.js
import { A } from './A'
import { myA } from './A' // Doesn't work!
import { Something } from './A' // Doesn't work!

To make these work, you would add a corresponding named export to A:

// A.js
export const A = 42
export const myA = 43
export const Something = 44

A module can only have one default export, but as many named exports as you'd like (zero, one, two, or many). You can import them all together:

// B.js
import A, { myA, Something } from './A'

Here, we import the default export as A, and named exports called myA and Something, respectively.

// A.js
export default 42
export const myA = 43
export const Something = 44

We can also assign them all different names when importing:

// B.js
import X, { myA as myX, Something as XSomething } from './A'

The default exports tend to be used for whatever you normally expect to get from the module. The named exports tend to be used for utilities that might be handy, but aren’t always necessary. However it is up to you to choose how to export things: for example, a module might have no default export at all.

This is a great guide to ES modules, explaining the difference between default and named exports.



Related Topics



Leave a reply



Submit