Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does {$variable} works in PHP?

When I want to use a variable value within a string, I connect them with . (dot) operator.

I see that some people use {$variable} within a string instead.

So..my example:

"my name is ".$variable

some people use it:

"my name is {$variable}"

What is the difference between above two examples?

like image 257
Moon Avatar asked Feb 28 '11 04:02

Moon


1 Answers

It's used when you want to append a string to the value in the variable inside a string.

$variable = 'hack';

// now I want to append 'ed' to $variable:    

echo "my name is {$variable}";   // prints my name is hack

echo "my name is {$variable}ed"; // prints my name is hacked

echo "my name is $variable";     // prints my name is hack

echo "my name is $variableed";   // Variable $variableed not defined.
like image 148
codaddict Avatar answered Oct 05 '22 03:10

codaddict