Page 1 of 1

Braces placed around variables

Posted: Wed Feb 04, 2009 2:10 pm
by mickeyunderscore
Hi, this is something I've seen in peoples' code before:

Code: Select all

echo "This is a number: {$number}";
//or
${number}
etc. I'm just curious as to what the braces do when placed around a variable?

As I often see them in places where the code works with or without them, I haven't been able to figure out exactly what they are for!

Re: Braces placed around variables

Posted: Wed Feb 04, 2009 3:05 pm
by Ziq
In this situation it is not necessary. But if you want to print something like this

Code: Select all

 
<?php
$arr['somename'] = '100500';
echo "this is somename: {$arr['somename']} .";
?>
 
you have to use this.

Re: Braces placed around variables

Posted: Wed Feb 04, 2009 3:24 pm
by mickeyunderscore
Oh I see! I always wondered how to echo an array index within double quotes, I had no idea that braces could be used for it.

That will make it easier to use mysql_fetch_assoc results.

Re: Braces placed around variables

Posted: Wed Feb 04, 2009 3:39 pm
by Ziq
I think it is not good idea. This syntax more readable

Code: Select all

 
<?php
$string = 'This is array value: '.$array['some'];
 
But it's just IMHO.