SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot Nate's Avatar
    Join Date
    Sep 2001
    Location
    BC, Canada
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    {$var} vs. " . $var . "

    I'm curious. Whats the difference between using:

    echo "var equals {$var} in this script.";

    or

    echo "var equals " . $var . " in this script.";

    Is there any difference performance-wise? Is it just personal preference?
    NATHAN WRIGHT
    PHP Developer, Simple Station

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, i guess it's just preference. i prefer the first. the second looks more "busy." in that first example:

    PHP Code:
    echo "var equals {$var} in this script."
    you don't need the {} around $var in that case. this is fine:

    PHP Code:
    echo "var equals $var in this script."
    the {}'s are only needed in a string if you want to put other letters are numbers "up against" the end of the variable. say you were timing something, and you want the result to be "5s" for 5 seconds. this wouldn't work:

    PHP Code:
    echo "$secs"
    because PHP would look for a variable called $secs. to use the $sec variable, but keep the "s" directly next to $sec's value, you'd use this:

    PHP Code:
    echo "{$sec}s"
    hope that helps.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Zealot Nate's Avatar
    Join Date
    Sep 2001
    Location
    BC, Canada
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I know it isn't necessary, I just like having an obvious distinction. Thanks.
    NATHAN WRIGHT
    PHP Developer, Simple Station

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "{" "}" brackets come in handy when you want to print out something like:

    $_POST['files']['name']

    Then you can use:

    PHP Code:
    echo "This file is called {$_POST['files']['name']}"
    If you did not use the brackets you would have to use . to concat the strings.

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Chris82

    Then you can use:

    PHP Code:
    echo "This file is called {$_POST['files']['name']}"
    If you did not use the brackets you would have to use . to concat the strings.
    yeah... i found out the hard way when i got those "expected T_STRING or T_NUMBER..." errors...

    especially useful in SQL queries using array variables...
    PHP Code:
    $query="SELECT * from my_table WHERE id={$_POST['id']}"

  6. #6
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like the second method just 'cos EditPlus turns the vars to pretty colours if I put them outside the quotes, it also allows me to use single quotes.
    Work smarter, not harder. -Scrooge McDuck

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And don't forget:

    echo 'var equals ', $var, ' in this script.';

    Technically the fastest of the lot (for what that is worth )

    Rather than concatenate the strings and then output the concatenated string, the above just sends each comma seperated arguement to stdout.

    Also note I used single quotes as there is no variable substitution required, the strings can be treated as literals and don't need to be parsed for embedded variable substitution.


  8. #8
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also note I used single quotes as there is no variable substitution required,
    A good point to remember rather than just defaulting to " " . I find remembering this also keeps me aware of what I'm working with: i.e.Know your data etc...

    Regards,

    Pete


  9. #9
    SitePoint Evangelist ckchin's Avatar
    Join Date
    Mar 2002
    Location
    msia
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great. I did not realise that I can use others besides echo "var equals " . $var . " in this script.";

    Can any expert out there summarize this thread about what we can use and in what case we need to use this and not that.
    And don't forget:
    echo 'var equals ', $var, ' in this script.';
    Technically the fastest of the lot (for what that is worth )
    I need to digest this first, and try this out.

  10. #10
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a good article on using strings in php,
    http://www.zend.com/zend/tut/using-strings.php

    And the manual page for echo is good too:
    http://www.php.net/manual/en/function.echo.php


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
  •