Lesson 4.6. PHP quotes

In PHP, character strings are surrounded by quotes. PHP accepts two syntaxes, single quotes ('') and double quotes ('' ). However, these two syntaxes are not identical.

Single quote

The use of single quotes is the basic syntax for declaring a string. But beware, the string will be recorded as it is, i.e. special characters (such as carriage return \n) will not be escaped (interpreted) and will consist of two characters: a backslash \ followed by the character n.

The only character that can be escaped is the single quote with this syntax \'.

Double quotes

The double quotes allow the escape of special characters and especially interpretation of variables. If the string contains PHP variables, this variables will be replaced by their values. Likewise, if special characters (\ n, \ r, \ $, ...) are present in the string, they will be replaced by their ASCII code.

Note that when tables with indexes are encapsulated in strings, they must be escaped with curly brackets as on the following example:


This example highlight the different syntaxes:

$variable = 4;
$table = array (10,20,30);

// Display $variable (single quotes)
echo 'Variable: $variable\n';

// Display 4 (double quotes)
echo "Variable: $variable\n";

// Display 20 (table with double quotes and curly brackets)
echo "Table: {$table[1]}\n";

See how the first carriage return is displayed and not escaped.

Single or double quotes?

The question of choosing a syntax arises. I recommend to use as much as possible single quotes because double quotes take longer to process since the PHP interpreter have to parse and analyse the string for escaping variables and special characters.


Let's considere the following PHP with a $a variable. The goal of this exercice is to display the value of $a in several ways:

$a = 1234.56;

// Display a=1234.56 using single quotes

// Display a=1234.56 using double quotes


<!-- Display a=1324.56 outside PHP script -->


Here is the expected result:

Expected result for the PHP quote exercice

See also

Last update : 09/17/2022