Braces placed around variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Braces placed around variables

Post 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!
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Braces placed around variables

Post 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.
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Braces placed around variables

Post 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.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Braces placed around variables

Post 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.
Post Reply