Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to wrap curly braces around a variable

Tags:

php

I don't know how to explain this but in simple terms I have seen people using {$variable} when outputting values. I have noticed that {$variable} doesn't work everything. When should we use {$variable}?

like image 725
Red Virus Avatar asked Feb 24 '16 09:02

Red Virus


People also ask

Should curly braces appear on their own line?

We strongly recommend you format your curly brackets following Java conventions: Do not put a new line before an opening curly bracket. Always start a new line after an opening curly bracket. A closing curly bracket should always belong on a separate line, except for else statements.

What is curly braces around variable in JavaScript?

Note: Curly brackets '{ }' are used to destructure the JavaScript Object properties.

What is the function of curly braces?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.


3 Answers

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/

like image 155
Fakhruddin Ujjainwala Avatar answered Sep 23 '22 07:09

Fakhruddin Ujjainwala


A couple of years late but may I add:

You can even use variable in curly braces to access methods of an Object from a Class dynamically.

Example:

$username_method = 'username'; $realname_method = 'realname';  $username = $user->{$username_method}; // $user->username; $name = $user->{$realname_method}; // $user->realname 

Not a good example but to demonstrate the functionality.

Another Example as per @kapreski's request in the comments.

/**Lets say you need to get some details about the user and store in an  array for whatever reason.   Make an array of what properties you need to insert.  The following would make sense if the properties was massive. Assume it is **/  $user = $this->getUser(); //Fetching User object  $userProp = array('uid','username','realname','address','email','age');  $userDetails = array();  foreach($userProp as $key => $property) {     $userDetails[] =  $user->{$property};        }  print_r($userDetails); 

Once the loop completes you will see records fetched from user object in your $userDetails array.

Tested on php 5.6

like image 33
Sanjok Gurung Avatar answered Sep 22 '22 07:09

Sanjok Gurung


As far as I know, you can use for variable $x

echo "this is my variable value : $x dollars";

… but if you don't have any spaces between the variable and the text around it, you should use {}. For example:

echo "this is my variable:{$x}dollars";

because if you wrote it $xdollars it will interpret it as another variable.

like image 44
Amr Mahmoud Ezzat Avatar answered Sep 23 '22 07:09

Amr Mahmoud Ezzat