psquillace Posted December 22, 2007 Share Posted December 22, 2007 Hello All: I have a newb question here. In learning php I seem to notice that we use the Curly Braces for the IF/ELSE statements but I have seen them used to seperate variables in like echo "<b>" . $id . ": {$name} </b>"; now $name is a variable and I think I understand why it my be like that but in leaning from a book, I am a bit confused as to why the author decided to throw that line together that way? Any thoughts or advice would be appreciated. Paul Quote Link to comment https://forums.phpfreaks.com/topic/82851-solved-when-do-you-use-curlies/ Share on other sites More sharing options...
papaface Posted December 22, 2007 Share Posted December 22, 2007 echo "<b>" . $id . ": {$name} </b>"; Tells php that there is variable in the quotes and therefore PHP needs to parse the variable. Quote Link to comment https://forums.phpfreaks.com/topic/82851-solved-when-do-you-use-curlies/#findComment-421376 Share on other sites More sharing options...
wildteen88 Posted December 22, 2007 Share Posted December 22, 2007 Wrapping variables in curly braces does have it's uses, curly braces are not just for defining code blocks. Curly braces are normally used when echoing a variable which is an array within a string, eg: echo "$my_arr['some_key'] bla bla bla"; The above code will result in an error, however if you wrapped the variable within braces the code will work fine: echo "{$my_arr['some_key']} bla bla bla"; Quote Link to comment https://forums.phpfreaks.com/topic/82851-solved-when-do-you-use-curlies/#findComment-421378 Share on other sites More sharing options...
roopurt18 Posted December 22, 2007 Share Posted December 22, 2007 I use them wherever they are allowed, even when their use is optional. Quote Link to comment https://forums.phpfreaks.com/topic/82851-solved-when-do-you-use-curlies/#findComment-421383 Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 To elaborate on what wildteen said, using the curly braces is also known as complex syntax, not complex because of the syntax itself but because you can make complex expressions. Here is another instance where you'd want to use curly braces around the variable name: <?php $fruit = 'banana'; echo "John has two $fruits"; // won't work, PHP will look for a variable called $fruits echo "John has two {$fruit}s"; // works, PHP will look for a variable called $fruit ?> Like roopurt I generally use curly braces around ALL variables which are inside strings for two reasons: 1) I think it looks nicer. 2) It ensures that it will always work (see wildteen's and my example). Quote Link to comment https://forums.phpfreaks.com/topic/82851-solved-when-do-you-use-curlies/#findComment-421385 Share on other sites More sharing options...
psquillace Posted December 23, 2007 Author Share Posted December 23, 2007 wow, thanks guys for all your help on this as my last lesson threw me for a loop when I saw that and did not have anything in my notes on it. Thanks again, Paul Quote Link to comment https://forums.phpfreaks.com/topic/82851-solved-when-do-you-use-curlies/#findComment-421399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.