We use single quotes and double quotes when defining text type data in PHP. So do you know the differences between them? In this lesson, we will learn the differences between single quotes and double quotes in PHP.

Single Quotes and Double Quotes in PHP

For data specified with single quotes, PHP prints the entire content almost "as is". Variables and most escape sequences are not interpreted. As an exception, you can have it interpreted with a backslash (escape character).

Data specified with double quotes is not printed as is. The data is interpreted and evaluated if there is a variable in it.

PHP Single Quote and Double Quote Examples

Using Variables in Text Data with Single Quotations

We mentioned that PHP prints the content almost as is for data specified with single quotes. If we use a variable in the data we define with single quotes, the data of the variable will not be printed. The variable name we use is printed as is. So if we want the variable data to be printed, we must use double quotes.

$my_name = "Onur Özden";
echo 'Benim adım $my_name'; // Çıktı: Benim adım $my_name

Using Quotes Within Quotes

If we use quotes within quotes and do not use an escape sequence, PHP returns an error output at Parse Error level. We can easily tell that there is a problem from the color scheme in the text editor or IDE we are using.

$var1 = 'Ben PHP'yi öğreniyorum.'; // Çıktı: Parse error: syntax error, unexpected 'yi' (T_STRING)
$var2 = "Ben PHP"yi öğreniyorum."; // Çıktı: Parse error: syntax error, unexpected 'yi' (T_STRING)

PHP escape characters We will cover the subject (escape sequences) in this lesson.

Using Variables in Text Data

Another important point is that you use curly brackets to isolate the name of the variable we want to evaluate. Let's add a variable to the text (string) type data as follows.

$my_name = "Onur Özden";
echo "Benim adım $my_name";

Such use is not recommended for readability. Recommended usage is as follows.

$my_name = "Onur Özden";
echo "Benim adım {$my_name}";