Inserting Variables into Strings in PHP
In PHP, single and double quotes for strings are not entirely equivalent. The fact is that variables can be inserted into double-quoted strings - and the value of these variables will be substituted in their place.
Let's try it in practice. Suppose we have some variable:
<?php
$str = 'aaa';
?>
First, let's insert this variable into some string using the concatenation operation:
<?php
$str = 'aaa';
echo 'xxx ' . $str . ' yyy';
?>
Now let's change the quotes of our string to double quotes and insert the variable into it:
<?php
$str = 'aaa';
echo "xxx $str yyy";
?>
Simplify the following code:
<?php
$name = 'user';
echo 'hello, ' . $name . '!';
?>