Single or Double Quote String literals in PHP

String literals in PHP can be either double-quoted or single-quoted. Both work fine. However, the single quote string literal comes with few limitations.

$name = "Alex";
$name2 = 'Alex';

Limitations of single quote string literals in PHP

In our example above, the second variable $name2, is a single quote string literal. Hence, its main limitations are:-

  1. Single quote string literals don’t understand most special characters,
  2. They don’t do string interpolation

String interpolation with double-quote string literals

Interpolation is the capacity to have a variable inside our string and its value will be displayed. For instance, we might want to print a message with the name of the person inside the string. We can do that:-

$name = "Abebe";
$greeting = "Hello $name";

Because of the lack of escape characters and interpolation, I always stick with double-quote strings.

Your subscription could not be saved. Please try again.
Your subscription has been successful.

Learn Modern PHP

Subscribe to learn modern PHP in the most simple way.