Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    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.

  • Users who have thanked Thyrosis for this post:

    Redd4 (11-28-2012)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • The Following 2 Users Say Thank You to tangoforce For This Useful Post:

    Custard7A (11-29-2012), Redd4 (11-28-2012)

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    Last edited by tangoforce; 11-28-2012 at 12:37 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    Thyrosis (11-29-2012)

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts
    wow, thanks guys. Tangoforce thats a sweet little demonstration there, bravo

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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


  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    Last edited by tangoforce; 11-28-2012 at 05:32 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    Thyrosis (11-29-2012)

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    No worries
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    No worries, glad to help
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •