43
votes

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}?

3
That's not vanilla php, that might be php framework syntax, for example blade template engine from Laravel 5 uses {{$variable}} to print stuff.Asur
@asur that is incorrect. This is known as complex (curly) syntax.Francesco de Guytenaere
@Ciccio Indeed but I think he means curly braces by their own not inside a printAsur

3 Answers

80
votes

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/

16
votes

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

1
votes

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.