SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Nov 2001
    Location
    Fort Lauderdale
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about the use of Curly Braces

    I've been working with PHP for awhile, but usually just modifying existing PHP code. I'm currently taking a class on lynda.com to brush up and to make sure i have good coding habits.

    Here is an example they give, it's just an example of an echo statement.. and they said to use curly braces when you want to attach a word or statement to a variable with no space.

    Here is the example:
    PHP Code:
    <?php
    $my_variable 
    "Hello World";    
    echo 
    "{$my_variable}Again!.";
    ?>
    I just want to make sure, because im only familiar with using curly braces with things like "if else" statements, and never seen them around variables for things like echo statements.

    Once the PHP code starts getting complex, would this cause issues or is this good practice?

    Thank you in advance.

  2. #2
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats the way I do it, if I am not modifying the variable at all then I'll just curly bracket it into the statement. But if I was going to escape the strings then I'll set a new variable and then place it in the echo:

    //Not Modifying A Variable/Session
    echo "Hello, {$_SESSION['username']}";

    //Modifying A Variable/Session
    $username = stripslashes($_SESSION['username']};
    echo "Hello, $username";
    I like setting any modified variables at the top rather then concatenating them into the code.

    I developed CMSs, forums, blogs and never had a problem using the curly brackets.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most people seem to prefer avoiding anything tricky when it comes to putting variables inside of double quotes. The common way is to not do it, and instead use string concatenation.
    PHP Code:
    echo $my_variable "Again!."
    I think you should go with what's easy to read and understand. Personally, I think the example you posted is still clear. A lot of code editors will still highlight the variable inside the string making it very obvious(this forums syntax highlighter doesn't yet do this though).

  4. #4
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heya.

    I use the brackets for when I am passing an object through a MySQL query, which is usually only like:
    -- because I need the single quotes inside the get() so it doesn't think userID is a constant.
    PHP Code:
    $db->query("
                UPDATE users SET
                `hp` = `hp` - '
    $lose_hp',
                `total_battles` = `total_battles` + '1'
                WHERE `id` = '
    {$sess->get('userID')}' LIMIT 1
            "
    ); 
    Also I usually do the brackets if I'm echoing a lot of content, I don't want to break apart the echo with the dots sometimes so I use brackets. I despise the Echo>>>> thing so I do it this way.

    If Im not using many lines in notepad Ill just concenate the string.


    PHP Code:
    <?php

        
    echo "    
        <h2>
    {$usr['login']} of {$usr['empire']}</h2>
        <br />Level: 
    {$usr['level']}
        
        <br class='clear' />
        
        <div class='graph'>
        <strong class='bar' style='width: 
    {$usr['hp']}%;'>HP {$usr['hp']}</strong>
        </div>
    "
    ;
    // etc..


Bookmarks

Posting Permissions

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