Concat single quote in php

The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string.

These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:

'\''

While if you use double quotes you have to escape other characters:

"\$"

But it also allows for some nifty things like adding a new-line to the end:

"my string\n"

With single quotes you would have to do a concatenation:

'my string' . chr(10)
'my string' . "\n"

Generally, single quotes are faster because they are "dumb".

However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided.

A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language.

Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times.

Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using.

Consider this:

$str1 = 'asdf';
$str2 = 'qwer';

// this will result in more memory being allocated for temporary storage
echo $str1 . $str2;

// this will not allocate as much memory as the previous example
echo $str1;
echo $str2;

This is an article I wrote a while ago on my old blog when I first started learning PHP. I'll repost it here, in case it helps anyone :)

If you want to save text as a variable or display it on the screen (with say, echo or print), you have to surround the text in quotes. Surrounding text by quotes makes that text a string.

You can use either single quotes (' ') or double quotes(" "), but there are some important differences, which I'll go over in this post.

Single Quotes

Single quotes are the simplest way to make a string. They just display what they are given, no bells and whistles, no special "powers" like being able to show variable values (see below in the Double Quotes section).

// Using single quotes to save a string in a variable:
$recipe_title = 'Meatball Spaghetti';

// Using single quotes to write something on the screen:
echo '

Meatball Spaghetti

'
; // The line above will get output as-is in your code: <h2>Meatball Spaghettih2>

Enter fullscreen mode Exit fullscreen mode

Line breaks with single quotes

If you need to display text on multiple lines, you can use line breaks within the quotes to achieve this. For example:

print '
  • Flour - 300 grams
  • Butter - 200 grams
  • Water - 100 ml
'
;

Enter fullscreen mode Exit fullscreen mode

This will be output as:

  • Flour - 300 grams
  • Butter - 200 grams
  • Water - 100 ml

Enter fullscreen mode Exit fullscreen mode

If you try to use multiple print or echo on multiple lines, it won't work the same way. For example:

print '
    '; print '
  • Flour - 300 grams
  • '
    ; print '
  • Butter - 200 grams
  • '
    ; print '
  • Water - 100 ml
  • '
    ; print '
'
;

Enter fullscreen mode Exit fullscreen mode

This will be output as:

  • Flour - 300 grams
  • Butter - 200 grams
  • Water - 100 ml

Enter fullscreen mode Exit fullscreen mode

Special characters and escape characters with single quotes

You only have two options when it comes to single quotes:

One: \' to escape a single quote within a single-quoted string

You would need to escape the ' character if you want to include it in the string. For example:

echo 'Za\'atar is a Middle Eastern spice mix.';

Enter fullscreen mode Exit fullscreen mode

This would properly output the following without causing an error.

Za'atar is a Middle Eastern spice mix.

Two: \\ to escape the backslash—the escape character—within the string

You could use

echo 'A \\ is called a "backslash."';

Enter fullscreen mode Exit fullscreen mode

To print

A \ is called a "backslash."

*It might depend on the compiler, but when I was testing this out,  \ seems to work by itself too. But, if for some reason you want to display \\, you might need to use something like echo '\\\'; or echo '\\\\';. Since it's a special character, it's probably best to escape it just in case.

Double Quotes

One big difference about double quotes vs single quotes is that you can use double quotes to include variables directly inside the string. If you use single quotes, you would have to concatenate the pieces together. Let's see an example.

Let's say you have recipes and you save the titles into a variable called $recipe_title:

$recipe_title = 'Meatball Spaghetti';

Enter fullscreen mode Exit fullscreen mode

If you want to create the HTML for recipe titles so that they look like this (and you aren't embedding PHP directly into HTML files, in which you might use tags instead to spit out variables):

Meatball Spaghetti

Enter fullscreen mode Exit fullscreen mode

Using single quotes you need to add the different parts together:

echo '

' . $recipe_title . '

'
;

Enter fullscreen mode Exit fullscreen mode

With double quotes, however, you can put the variable directly inside the quotes:

echo "

$recipe_title

"
;

Enter fullscreen mode Exit fullscreen mode

Both methods work just fine, but using double quotes can save you some hassle.

Pro Tip:

Use curly braces to explicitly specify the end of a variable name when parsing it into a double-quoted string.

Trying to print 2 cups on line 2 below will give you an error because the code thinks the variable name is $unit_cups instead of $unit_cup:

$unit_cup = "cup";
print "Flour - 2 $unit_cups";

Enter fullscreen mode Exit fullscreen mode

To avoid errors like this, you can surround the variable name in curly brackets likes so:

$unit_cup = "cup";
print "Flour - 2 ${unit_cup}s";

Enter fullscreen mode Exit fullscreen mode

You can also do some more complex operations right in the double quotes, but that's beyond the scope of this article. To learn more about parsing complex operations within double quotes, check out the examples in the PHP manual.

--

By the way, like single quotes, you can add line breaks to your output by including line breaks within the string. For example,

print "
  • Gin - 3 ounces
  • Tonic - 4 ounces
  • Lime - 1 slice
"
;

Enter fullscreen mode Exit fullscreen mode

displays:

  • Gin - 3 ounces
  • Tonic - 4 ounces
  • Lime - 1 slice

Enter fullscreen mode Exit fullscreen mode

Special characters and escape characters with double quotes

Double quotes give you many more special characters to work with than single quotes, including the line break character.

  • \n for a new line
  • \t for a tab
  • \r for a carriage return
  • \$ for a dollar sign (otherwise it could be mistaken as variable)
  • \" for a double quote
  • See more in the PHP Manual

What Should I Use?

In general, you use either or, but you should be consistent with what type you use and when. For example, you might choose to use single quotes by default unless you need to use variables or special characters within the string.

You might think that because double quotes give you more features, that it would be better to use them all the time, but single quotes are probably better for simple strings because you don't need to escape special characters like dollar signs.

Can I use single quotes in PHP?

Yes. It is slightly faster to use single quotes. PHP won't use additional processing to interpret what is inside the single quote. when you use double quotes PHP has to parse to check if there are any variables within the string.

How do you add single quotes to a string?

The following are our strings with single and double quote. String str1 = "This is Jack's mobile"; String str2 = "\"This is it\"!"; Above, for single quote, we have to mention it normally like. However, for double quotes, use the following and add a slash in the beginning as well as at the end.

How can I add double quotes to a variable in PHP?

A double-quoted string will output \' with either a single or double backslash used with the apostrophe..
\" for a double quote..
\\ for a backslash..
\$ to render a dollar sign instead of expanding the variable..
\n for a new line..
\t for a tab..

What is the difference between single quotes and double quotes when assigning it to a string variable?

Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL.