String literals

PHP can create double- and single-quoted string literals. If double quotation marks are needed as part of a string, the easiest approach is to switch to the single-quotation style:

echo 'This works';
echo "just like this.";
// And here are some strings that contain quotes
echo "This string has a ': a single quote!";
echo 'This string has a ": a double quote!';

Escaping Quotation marks

Quotation marks can be escaped like this:

echo "This string has a \": a double quote!";
echo 'This string has a \': a single quote!';

One of the convenient features of PHP is the ability to include the value of a variable in a string literal. PHP parses double-quoted strings and replaces variable names with the variable's value. The following example shows how:

$number = 45;
$vehicle = "bus";
$message = "This $vehicle holds $number people";
// prints "This bus holds 45 people"
echo $message;

To include backslashes and dollar signs in a double-quoted string, the escaped sequences \\ and \$ can be used.

"a string with a \\ and a \$variable"

The single-quoted string isn't parsed in the same way as a double-quoted string and can print strings such as:

'a string with a \ and a $variable'

We discuss parsing of string literals in more detail in Section 2.6.