Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2012, 09:43 AM   PM User | #1
Redd4
New Coder

 
Join Date: Apr 2012
Posts: 32
Thanks: 20
Thanked 0 Times in 0 Posts
Redd4 is an unknown quantity at this point
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?





regards,
redd
Redd4 is offline   Reply With Quote
Old 11-28-2012, 10:50 AM   PM User | #2
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
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.
Thyrosis is offline   Reply With Quote
Users who have thanked Thyrosis for this post:
Redd4 (11-28-2012)
Old 11-28-2012, 10:51 AM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
The Following 2 Users Say Thank You to tangoforce For This Useful Post:
Custard7A (11-29-2012), Redd4 (11-28-2012)
Old 11-28-2012, 12:34 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 11-28-2012 at 12:37 PM..
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
Thyrosis (11-29-2012)
Old 11-28-2012, 02:35 PM   PM User | #5
Redd4
New Coder

 
Join Date: Apr 2012
Posts: 32
Thanks: 20
Thanked 0 Times in 0 Posts
Redd4 is an unknown quantity at this point
wow, thanks guys. Tangoforce thats a sweet little demonstration there, bravo
Redd4 is offline   Reply With Quote
Old 11-28-2012, 04:45 PM   PM User | #6
Redd4
New Coder

 
Join Date: Apr 2012
Posts: 32
Thanks: 20
Thanked 0 Times in 0 Posts
Redd4 is an unknown quantity at this point
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

Redd4 is offline   Reply With Quote
Old 11-28-2012, 05:25 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 11-28-2012 at 05:32 PM..
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
Thyrosis (11-29-2012)
Old 11-28-2012, 11:46 PM   PM User | #8
Redd4
New Coder

 
Join Date: Apr 2012
Posts: 32
Thanks: 20
Thanked 0 Times in 0 Posts
Redd4 is an unknown quantity at this point
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
Redd4 is offline   Reply With Quote
Old 11-29-2012, 01:50 AM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
No worries
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 11-29-2012, 03:34 PM   PM User | #10
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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.
Custard7A is offline   Reply With Quote
Old 11-29-2012, 09:04 PM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
No worries, glad to help
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:20 AM.


Advertisement
Log in to turn off these ads.