different methods of using vars in a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

different methods of using vars in a string

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

feyd wrote:A nice bonus is it processes a tiny bit faster.
any evidence of this? and why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do some testing to see it..
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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:
Post Reply