![]() |
question about braces
guys hey. I'm in the process of learning php, through video tutorials. something has come up that I'm confused about and is not explained. probably its very simple.
in the photo below, he put braces around $name but not around $id is there a specific advantage or disadvantage to this... why did he do that, is there a difference in how it will act? http://i50.tinypic.com/15fol6r.jpg regards, redd |
It's got something to do with escaping characters within variables.
For more information, I've found http://stackoverflow.com/questions/2...-string-in-php (which links to PHPs String manual page at http://php.net/manual/en/language.types.string.php. Personally I always use {$variable} when including it in a string. |
He's simply showed you two ways of using a variable inside a string.
Take a look at the link in my signature about T_CONSTANT_ENCAPSED_STRING to see more about this. Not many online PHP teaching sites explain it in detail. |
Almost forgot..
In my link I don't actually explain braces but basically what they do is to tell PHP to treat that variable independently to the rest of the string. This is useful if you're using an array in a string (especially multidimensional arrays) or a string which contains a character such as _ which would be seen as part of the variable if you didn't seperate them. PHP Code:
|
wow, thanks guys. Tangoforce thats a sweet little demonstration there, bravo
|
if I may, can i ask another question plz
I understand that single quotes are used in case where there might be double quotes in the string, but this chap has used single quotes for the key (?) and doubles around the string, wondering why http://i47.tinypic.com/34si2dw.jpg |
No, single quotes are not used if there are doubles in the string.
Single quotes are used if there is no VARIABLES in the string that need to be replaced. Double quotes are used when there ARE VARIABLES in the string that need to be replaced. Simply put: PHP Code:
For the double quotes in a key that I mentioned.. PHP Code:
As mentioned above, "Kevin" is not wrong but it will force PHP to examine the string to see if it needs to work its magic and replace any variables. As there is not a variable in there then single quotes would be better. |
ok I will take that on board. I'll copy and paste it to keep a note of it also.
thanks for taking the time |
No worries :thumbsup:
|
Aha, now I know the difference between single and double quotes, as well as these braces in strings. I've been learning PHP for nearly two years, and I never knew that. As you mentioned tangoforce, little quirks like that are oft overlooked by PHP tutorials, even ones about syntax in specific. Just saying thanks. :)
|
No worries, glad to help :thumbsup:
|
| All times are GMT +1. The time now is 09:50 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.