Page 1 of 1

different methods of using vars in a string

Posted: Wed Apr 20, 2005 8:04 am
by malcolmboston
ive noticed lately that people use different methods than me when printing there vars for eg i do

Code: Select all

print "Hello {$username}";
some people use a . $username . etc method etc

im just wondering if theres any major differences between the differeing methods and if i should be doing it the way i am and always done for eg an sql string for me would look like

Code: Select all

$query = "SELECT something FROM somewhere WHERE something = '{$value}'";
$result = mysql_query($query) or die (mysql_error());
any feedback would be appreciated

Posted: Wed Apr 20, 2005 8:12 am
by feyd
I prefer the concatenation method, mostly because it's easier to quickly see where variables are. A nice bonus is it processes a tiny bit faster.

Posted: Wed Apr 20, 2005 8:20 am
by vigge89
I'm using both "{$var}" and ''.$var.'', depending on what I'm coding. Using concatenation, it's usually easier to spot variables, but when the strings contain multiple quotes, I prefer using "text '{$var}'" for readability and editing reasons.

Posted: Wed Apr 20, 2005 8:34 am
by malcolmboston
feyd wrote:A nice bonus is it processes a tiny bit faster.
any evidence of this? and why?

Posted: Wed Apr 20, 2005 8:51 am
by feyd
do some testing to see it..

Posted: Wed Apr 20, 2005 9:31 am
by vigge89
malcolmboston wrote:
feyd wrote:A nice bonus is it processes a tiny bit faster.
any evidence of this? and why?
By using single quotes, PHP don't have to look for any variables or arrays.
If you meant something else, then sorry :roll: