CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   question about braces (http://www.codingforums.com/showthread.php?t=283128)

Redd4 11-28-2012 09:43 AM

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

Thyrosis 11-28-2012 10:50 AM

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.

tangoforce 11-28-2012 10:51 AM

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.

tangoforce 11-28-2012 12:34 PM

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:

//Example 1
$Array['chaser'] = 'dog';
$Array['neighbours']['pet'] = 'cat';//Multidimensional array

Print "The {$Array['chaser']} chased the {$Array['neighbours']['pet']}";// The dog chased the cat

//Example 2
$Array[1] = 'under';
$Array[2] = 'score';

Print 
"This is how you might demonstrate using an {$Array[1]}_{$Array[2]}";
//This is how you might demonstrate using an under_score 

As you can see, php will treat those in braces inside a string as if they are seperate. Had I not used braces in the second example either side of the _ then PHP wouldn't have worked its magic there.

Redd4 11-28-2012 02:35 PM

wow, thanks guys. Tangoforce thats a sweet little demonstration there, bravo

Redd4 11-28-2012 04:45 PM

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

tangoforce 11-28-2012 05:25 PM

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:

$Name 'Adrian';

Print 
'Hello $Name'// Hello $Name

Print "Hello $Name"// Hello Adrian 

As for single quotes used in the key and double quotes used for the name-string, well thats just down to the author. Technically it will work (and you can use double quotes in the key too) but using double quotes where they are not needed wastes CPU resources. If you have a script that is 2-3000 lines long and you're using double quotes all the way through when you could just use single quotes, thats going to take more time for the script to run and finish.

For the double quotes in a key that I mentioned..
PHP Code:

$Variable 'Item';
$Array["$Variable"] = 'Apple'//Will work but not needed because..

$Array[$Variable] = 'Apple'// Better.. BUT what if we need a second word in there (or an underscore)?? ..

$Array["$Variable Type"] = 'Apple'// OR..

$Array["{$Variable}_Type"] = 'Apple'// OR..

$Array[$Variable .'_Type'] = 'Apple'

In reality there are many ways to handle strings and variables but its just a case of picking the one that is best for the situation. I ALWAYS use single quotes for anything that has no variables inside it.

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.

Redd4 11-28-2012 11:46 PM

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

tangoforce 11-29-2012 01:50 AM

No worries :thumbsup:

Custard7A 11-29-2012 03:34 PM

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. :)

tangoforce 11-29-2012 09:04 PM

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.