In PHP, double quotes are automatically parsed for any variables contained inside, but single quotes are not. Therefore:
$myVar = 21; echo "myVar: $myVar"
This displays the text: myVar: 21
While:
$myVar = 21; echo 'myVar: $myVar'
This prints the text: myVar: $myVar
One problem with your code is that in HTML element attribute values ββmust be enclosed in double quotes, not single quotes. I know that some browsers will take this form (or even no quotes at all), but this is not the right method.
There are various ways to achieve what you want.
First method: escaping strings with two quotes:
$myVar = 21; echo "<div id=\"$myVar\"></div>";
While this may be a rather inelegant solution, it will work.
Method two: use string concatenation with single (or double) quotation marks:
$myVar = 21; echo '<div id="' . $myVar . '"></div>';
This offers a better IMO solution because you can use function calls or any other PHP code there if you want.
Attention:
Please note that when you are not sure about the contents of $myVar (i.e. the user enters it), its direct HTML code is a cross-site scripting (XSS) security vulnerability. Imagine that the user enters something like this:
lol"><script>alert('XSS!');</script></div><div id="lol2
This will result in the following content as a result of the HTML code:
<div id="lol"><script>alert('XSS!');</script></div><div id="lol2"></div>
This is just a good example, but an attacker can easily use the same technique to steal a user's cookies (to pretend that they are logged in as that user). The message here is that if you are not 100% sure of the contents of a variable, do not paste it directly into the HTML code. Instead, call htmlspecialchars($myVar) . This will result in the following:
$myVar = $_POST['whatever']; echo '<div id="' . htmlspecialchars($myVar) . '"></div>';