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:-
- Single quote string literals don’t understand most special characters,
- 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.