In PHP, string written in single quotes is displayed 'as it is'. Variables and most of escape sequences are not interpolated.
Example,
$name = 'Farhan';
echo 'Welcome $name';
// Output: 'Welcome $name';
Whereas in double quotes, variables and escape sequences are interpolated.
$name = 'Farhan';
echo "Welcome $name";
// Output: "Welcome Farhan"
Edit #1:
Thanks @cseder for correcting to use interpolated
instead of interpreted
.
Top comments (2)
To be a nitpicker, the word to use is interpolated / interpolation, as in:
You can use interpolation to interpolate (insert) a variable within a string. Interpolation works in double quoted strings and the heredoc syntax.
Everything in PHP gets interpreted...
For completeness, PHP also has the complex / curly bracket syntax for embedding expressions (yielding a return value) in a string.
Any scalar variable, array element or object property with a string representation can be included.
Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }.
Since { can not be escaped, this syntax will only be recognized when the
$ immediately follows the {.
Thanks @cseder for correcting me out.
And adding some valuable examples too ✌