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 '<h1>Meatball Spaghetti</h1>';
// The line above will get output as-is in your code:
<h1>Meatball Spaghetti</h1>
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 '<ul>
<li>Flour - 300 grams</li>
<li>Butter - 200 grams</li>
<li>Water - 100 ml</li>
</ul>';
This will be output as:
<ul>
<li>Flour - 300 grams</li>
<li>Butter - 200 grams</li>
<li>Water - 100 ml</li>
</ul>
If you try to use multiple print
or echo
on multiple lines, it won't work the same way. For example:
print '<ul>';
print '<li>Flour - 300 grams</li>';
print '<li>Butter - 200 grams</li>';
print '<li>Water - 100 ml</li>';
print '</ul>';
This will be output as:
<ul><li>Flour - 300 grams</li><li>Butter - 200 grams</li><li>Water - 100 ml</li></ul>
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.';
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."';
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';
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 <?php ?>
tags instead to spit out variables):
<h1>Meatball Spaghetti</h1>
Using single quotes you need to add the different parts together:
echo '<h1>' . $recipe_title . '</h1>';
With double quotes, however, you can put the variable directly inside the quotes:
echo "<h1>$recipe_title</h1>";
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";
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";
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 "<ul>
<li>Gin - 3 ounces</li>
<li>Tonic - 4 ounces</li>
<li>Lime - 1 slice</li>
</ul>";
displays:
<ul>
<li>Gin - 3 ounces</li>
<li>Tonic - 4 ounces</li>
<li>Lime - 1 slice</li>
</ul>
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.
Top comments (0)