1

I am currently looking for a way to insert JavaScript into my php code, however, I cannot do it within an echo. I am considering using the following workaround:

<?php
   some code
?>

<script type="text/javascript>
   js things
</script>

<?php
   more code
?>

I am, however, a bit worried about opening and closing the <?php part. Is this considered bad practice and why?

AM-
  • 871
  • 1
  • 10
  • 20
  • 8
    Nope it's pretty normal practice. Infact, I'd encourage it. – ahren Dec 06 '12 at 21:15
  • 1
    But I would avoid mixing any substantial logic with HTML. – Waleed Khan Dec 06 '12 at 21:16
  • 10
    @NolwennLeGuen Whoever told you that should probably be slapped; preferably with a heavy leather glove. – Kermit Dec 06 '12 at 21:16
  • 3
    Closing PHP is bad practice if it's a pure server-side script because if there are characters (like a return) after `?>` it will output them to the browser and potentially prevent headers from being sent (like Location: foo.com) – Brian Cray Dec 06 '12 at 21:18
  • ^ Right, it's usually common convention not to close the last PHP tag in your PHP-HTML. – elclanrs Dec 06 '12 at 21:19
  • You could run into issues if you're not careful as others have pointed out but I wouldn't say that opening and closing PHP tags is bad practice. Using PHP to echo JavaScript or HTML would be bad practice. – War10ck Dec 06 '12 at 21:23
  • @BrianCray a single return after ?> is no problem because PHP will not output the next return after the closing ?>. Just any following return or character will be a problem. Just for correctness. – Benjamin Paap Dec 06 '12 at 21:30

4 Answers4

9

Though I would warn you to be frugal when mixing different script types, this is very normal and acceptable in programming. If you can find a way around it, I would suggest it, but this is far better than attempting to echo Javascript from PHP.

Liftoff
  • 24,717
  • 13
  • 66
  • 119
  • Personally I think it's a horrible practice. I've never had a use for more than one PHP tag in a PHP script. Each PHP file contains one PHP tag and one PHP class (and no JavaScript, and no HTML). HTML goes in HTML files. JavaScript goes in JavaScript files. PHP decides what to serve up, combines together some document fragments, and sends it across the wire. Separation of concerns. – Dagg Nabbit Dec 06 '12 at 21:32
  • @GGG, sounds like a PHP dev utopia. But don't you contradict yourself, what do you mean by *document fragments*. – Jason McCreary Dec 06 '12 at 21:55
  • @JasonMcCreary, in the past I used XML documents / document fragments, and combined them into XHTML documents. Now that XHTML (and XML, sort of) have fallen out of popularity, I just use HTML document fragments. – Dagg Nabbit Dec 06 '12 at 22:00
  • +1 for the word "frugal" as well. Definitely half the pleasure of this answer – barakadam Dec 07 '12 at 00:15
4

Is opening and closing PHP considered bad practice?

No.

You enter the gray area of bad practice when you start:

  • Long running blocks of PHP that output HTML via echo, print, etc.
  • Using short open tags.
  • Don't use semi-colons before the closing tag.

I encourage you to read about php tags and instruction separation.

In reference to JavaScript bad practice though, you may wish to separate your behavior from your content by using external JavaSCript (also more performant). Read more.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

No it is not bad practice. You should consider that your document is fundamentally HTML. So the PHP is pretty much "injecting" more stuff into it. If you did not close the PHP but used it to output the same content, you would do exactly the same thing, but in a more complicate manner and in a more processor intensive manner, as everything inside the PHP tags has to be parsed. On the contrary, what is outside these tags is pure HTML and is not parsed, but directly sent. So it is not bad practice. It is good practice!

barakadam
  • 2,179
  • 1
  • 16
  • 15
  • If you look at `` as a processing instruction, and use it as such, it's not horrible. The problem is, people will query the database and everything else right there in a PHP tag, in what should be the "view" for all intents and purposes. So maybe it's not bad design in and of itself, but it can certainly lead there. – Dagg Nabbit Dec 06 '12 at 21:40
0

No , it's not. Although I prefer keeping as much code at the top as I can to improve readability and maintenance of the script.

Léon

leonvv
  • 152
  • 4