Good and bad practices are just superstitions to comfort people who feel uneasy doing things they don't understand. For my money, anything goes as long as it makes sense and is practical.
In your particular example, I think using json_encode
inside the JS code is appropriate. After all, $my_var
is nothing but a variable for PHP, and JSON is a way to make that variable available to JS.
As a matter of personal choice, I don't use the shortened <?= ?>
form to echo variables, because I prefer to have the bits of PHP standing out clearly. But that's a matter of personal taste.