1

I just started to use PHP variables in my javascript (basic example below). I was wondering if this was bad practice and if there was a better way of handling this. Thanks!

  <script type="text/javascript">
    var my_var = <?php echo json_encode($my_var); ?>;
  </script>
Jamie
  • 1,909
  • 3
  • 22
  • 47
  • 3
    That's a fine practice :-) – gen_Eric Jan 16 '14 at 18:34
  • Please give more details. What kind of data are you sharing between languages? – John Kugelman Jan 16 '14 at 18:34
  • If that's what you need, then I dont see why not. – Alex Shilman Jan 16 '14 at 18:36
  • 1
    I think it's just personal preference. I suggest you use more **descriptive** variable names instead. – Amal Murali Jan 16 '14 at 18:36
  • Your approach is fine. The other approach would be an ajax request, but that is an extra request and breaks the synchronicity of your code. Also, instead of making a new global js var for each php variable, make `$my_var` an associative array instead (if you didn't already) so that it will `json_encode` into an object, this way making only a single js global variable. – Fabrício Matté Jan 16 '14 at 18:41
  • duplicate question http://stackoverflow.com/questions/10954341/is-it-bad-practice-to-echo-javascript-with-php – sas Jan 16 '14 at 18:50

1 Answers1

4

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.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • I wouldn't call "bad practices" just superstitions. Sure there are some practices which may have originally been considered bad due to superstition (e.g. [PHP's double-quoted strings](http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html)), but there are many bad practices which are actually bad, hurting performance and generating code smell. – Fabrício Matté Jan 16 '14 at 18:47
  • In fact, if it can't be justified as "bad", I wouldn't call it a bad practice. "Bad" is often relative to the use case and its needs. – Fabrício Matté Jan 16 '14 at 18:48
  • So yes, there is nothing "bad" with `json_encode` to output JS data as JSON is a subset of the JS object notation. But "bad practices are just superstitions" is far from true. For example, JS's `eval` has a proven negative effect on GCing and thus shouldn't be used unless there's no other practical way (which is hardly ever the case). – Fabrício Matté Jan 16 '14 at 18:57
  • @FabrícioMatté LOL As I see it, people doing things they don't understand is always bad, be it called good or bad practices. Half of these "practices" are just whims of fashion, especially when the subject offers ample room for arbitrary design choices. CSS is a good example, with the CSS nazis intent on murdering anyone who does not use their idiotic lists to make menus and jeering at any design that is not "fluid" enough for their taste. – kuroi neko Jan 16 '14 at 18:59
  • "doing things they don't understand is always bad" - true that, hence we tell them to RTFM first `=]`. And yeah, CSS is just for styling, if you're making your own CSS just make things look the way you want and that is it. For huge projects though, I can understand the adoption of BEM and other methodologies which improve the overall maintainability. – Fabrício Matté Jan 16 '14 at 19:02
  • 1
    @FabrícioMatté Agreed. You have to find a common ground between coworkers, but that does not apply to every single programmer on Earth :). – kuroi neko Jan 16 '14 at 19:05