No, it's usually a bad practice.
var someVariable = '<?php echo $someOtherVariable ?>';
This seems like it should work. And indeed it will if
$someOtherVariable doesn't contain a newline or a single apostrophe. However, if it contains
Early terminating of a string literal leads to invalid code after it:
var someVariable = 'O'Brian';
Invalid multiline string:
var someVariable = 'Hello
Looking at the code and determining whether the code is correct right now and will remain correct becomes very hard. What if the format of the data you're echoing changes? What if you get some data you didn't expect?
Impeded code analysis
Not only is it hard for humans to determine how a code would behave, but automated tools that are there to help you might also suffer. Some examples
- Syntax highlighters may break because of the mix of the two languages. This is often the first line of defence against defective code. For example, look at the line that says
'O'Brian' - you'd see that the highlighting is inconsistent between
- Tools that analyse code for correctness like ESLint or Tern.js among others will not be able to analyse code that's not there. Is
Hard to test code
Hard to debug