You can optionally use the {} parenthesis to delineate the variables if you wish, as I have done.
That way you can mix single and double quotes so that your output is valid html/js without worrying about PHP string concatenation quote requirements mucking the water up.
Eventually the “am I going mad?” feeling becomes more familiar, and you even get used to it for a while!
Then after many code cycles of paring code back and back again to find really silly errors that you should eventually have a light bulb moment and realise you should start at the bottom and get into the habit of writing a bit, testing it, forcing PHP to prove what you think SHOULD happen (or should NOT happen in many cases) then carrying on.
Extreme cases cause you to arrive at the door of Test Driven Development.
Good tricks to learn :
use var_dump() on all your variables, especially POST/GET and use echo to in order to inspect what is going on.
turn on error reporting on you development server
When working with JS and PHP :
inspect the html source code of your page
use heredoc syntax in PHP to assemble the code
get the JS working FIRST with hard-coded values, then replace the values with variables
check out the IDEs which support JS validation/markup
Mixing PHP/JS can be a real nightmare though - it compounds the possibilities for mistakes.