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?
| SitePoint Sponsor |

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
yeah, i guess it's just preference.i prefer the first. the second looks more "busy." in that first example:
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 "var equals $var in this script.";
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 "$secs";
hope that helps.PHP Code:echo "{$sec}s";
![]()
- Matt
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

Yeah, I know it isn't necessary, I just like having an obvious distinction. Thanks.
NATHAN WRIGHT
PHP Developer, Simple Station





The "{" "}" brackets come in handy when you want to print out something like:
$_POST['files']['name']
Then you can use:
If you did not use the brackets you would have to use . to concat the strings.PHP Code:echo "This file is called {$_POST['files']['name']}";
yeah... i found out the hard way when i got those "expected T_STRING or T_NUMBER..." errors...Originally posted by Chris82
Then you can use:
If you did not use the brackets you would have to use . to concat the strings.PHP Code:echo "This file is called {$_POST['files']['name']}";
especially useful in SQL queries using array variables...
PHP Code:$query="SELECT * from my_table WHERE id={$_POST['id']}";




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
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.
![]()


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...Also note I used single quotes as there is no variable substitution required,
Regards,
Pete



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.I need to digest this first, and try this out.And don't forget:
echo 'var equals ', $var, ' in this script.';
Technically the fastest of the lot (for what that is worth )
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