V Posted May 6, 2010 Share Posted May 6, 2010 Hello I'm following a learn php book and it shows two examples to put string values together. echo $somevariable. 'some text'; and echo "$somevariable some text"; The one with the double quotes seems easier to use. Does the way you write it affect anything or is it just a personal preference? Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/ Share on other sites More sharing options...
Psycho Posted May 6, 2010 Share Posted May 6, 2010 Personal preference. There are also the heredoc and nowdoc methods of defining variables. I suggest you take a look at this page: http://php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054214 Share on other sites More sharing options...
V Posted May 6, 2010 Author Share Posted May 6, 2010 thanks mj! I decided to use double quotes as my all time preference Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054218 Share on other sites More sharing options...
Psycho Posted May 6, 2010 Share Posted May 6, 2010 Yeah, I use both single and double as my preference depending on the context. If I want to include variables in the string I alomst always use double quotes and include the varaibles within curly braces to make it apparent that the value is a variable. The braces also prevent the variable from being misinterpreted if other characters must reside right next to the variable. $salutaion= "Hello {$username}!"; However, if there are no variables in the string, and especially if it will have HTML tags, I will use single quotes so I don't have to escape the double quotes int he HTML code $img= "<img src="pathtofile/logo.gif" />"; Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054220 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2010 Share Posted May 6, 2010 It is mostly personal preference. However, the first method generally results in more typing errors (we see the resulting syntax errors posted on the Forum) because there are more transitions between the different syntax usage when switching into/out of a quoted string. The second method is actually a little faster executing (if you have 10's of thousands of strings in a benchmark loop you will see a few milliseconds time difference.) I personally use the second method most of the time (fewer typing errors, fewer characters to type as well.) However if I am typing something simple, I will also use echo $variable . '<br />'; If you put array variables inside of a double-quoted string or have text adjacent to a variable name where php cannot determine where the text/variable name starts and ends, you need to put {} around the variable. Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054222 Share on other sites More sharing options...
Daniel0 Posted May 6, 2010 Share Posted May 6, 2010 The second method is actually a little faster executing (if you have 10's of thousands of strings in a benchmark loop you will see a few milliseconds time difference.) They'll compile to the same opcodes, so unless you aren't using an opcode cache (you should always use an opcode cache), this will only affect the first execution, and even then the performance gain/loss is negligible. Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054263 Share on other sites More sharing options...
V Posted May 6, 2010 Author Share Posted May 6, 2010 Yeah, I use both single and double as my preference depending on the context. If I want to include variables in the string I alomst always use double quotes and include the varaibles within curly braces to make it apparent that the value is a variable. The braces also prevent the variable from being misinterpreted if other characters must reside right next to the variable. $salutaion= "Hello {$username}!"; However, if there are no variables in the string, and especially if it will have HTML tags, I will use single quotes so I don't have to escape the double quotes int he HTML code $img= "<img src="pathtofile/logo.gif" />"; Thanks, I didn't know about the curly braces! If I understood, for example ${$amount} good to know! Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054278 Share on other sites More sharing options...
V Posted May 6, 2010 Author Share Posted May 6, 2010 It is mostly personal preference. However, the first method generally results in more typing errors (we see the resulting syntax errors posted on the Forum) because there are more transitions between the different syntax usage when switching into/out of a quoted string. The second method is actually a little faster executing (if you have 10's of thousands of strings in a benchmark loop you will see a few milliseconds time difference.) I personally use the second method most of the time (fewer typing errors, fewer characters to type as well.) However if I am typing something simple, I will also use echo $variable . '<br />'; If you put array variables inside of a double-quoted string or have text adjacent to a variable name where php cannot determine where the text/variable name starts and ends, you need to put {} around the variable. How about when you want to wrap some variables in HTML tags? For example, echo '<p>Posted on: '; echo date('H:i, jS F Y'); echo '</p>'; Does matter which method you use? Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054284 Share on other sites More sharing options...
teamatomic Posted May 6, 2010 Share Posted May 6, 2010 No, what you give an example of is a variable variable ${$var}. $pine='spruce'; ${$pine}='evergreen'; echo ${$pine}; echo "<br>"; echo $$pine; echo "<br>$spruce"; Wraping html in single quotes is fine. Also look at heredoc and its use of single and double quotes. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054296 Share on other sites More sharing options...
Psycho Posted May 6, 2010 Share Posted May 6, 2010 No, what you give an example of is a variable variable ${$var}. Yeah, teamtonic is right. You happened to pick one very unique case where using curly braces would not give you what you were looking for. In that particular case, you could omit the curly braces to get the output you were after. Here is an example where you would want to use the curly braces. Example: Let's say the application uses the user's first and last name with an underscore between them to autogenerate a folder name to store a user's data. If you wanted to display the folder name on the screen, this would not work: $first = 'Dave'; $last = 'Smith'; echo "$first_$last"; //Output: Smith That would try to echo the variables $first_ and $last. The variable $first_ doesn't exist and you would only be displaying the last name. Instead you could do this: $first = 'Dave'; $last = 'Smith'; echo "{$first}_{$last}"; //Output: Dave_Smith Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054361 Share on other sites More sharing options...
V Posted May 7, 2010 Author Share Posted May 7, 2010 Mj thanks for the examples! I will definitely use that in the future Quote Link to comment https://forums.phpfreaks.com/topic/200919-string-concatenation-vs-double-quotes/#findComment-1054417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.