Jump to content

String Concatenation vs double-quotes


V

Recommended Posts

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?

Link to comment
Share on other sites

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" />";

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.