PHP - include instead of ` – Brian Jul 01 '13 at 02:46

  • `JavaScript` is a client side and `PHP` is server side. So obviously, you need to load JavaScript as soon as the page request. Hence `` is better. – Mohit Bhansali Jul 01 '13 at 02:47
  • totally don't get what you said ò.o (actually I don't think server-side and client-side have something to do with this issue) – Diego T. Yamaguchi Jul 01 '13 at 03:02
  • Always the first one. Period. – Bitterblue Mar 09 '20 at 12:38
  • 4 Answers4

    4

    Ok, it took me second to figure out what you were asking. In your first choice you are outputing a script tag that links to your javascript, in the second you using PHP to include your javascript inline.

    Of the two choices, the first is by far the best. Assuming your page content is dynamic, due to browser caching, for every page a person downloads from you, the same javascript will be included everytime. If your javascript is 100kb in size, every page is now an extra 100kb. Over time this will add up for both your server and your clients.

    Including your Javascript (and CSS) by linkages allows the browser to cache pages, and only fetch what is necessary. This will similarly reduce the number of requests as a browser will only fetch what is necessary, which in most cases is just the HTML page.

    edit: What if the script is used on only one page?

    Still include the Javascript by a link, rather than inline. If you page is 100% static, but has thats not one page but many. And each request will get a new output, with the same replicated Javascript. Even if your page is pure-static HTML, still include it by a link as you never know when you might want to reuse the Javascript (or CSS) code.

    • wow, I totally missed about this. You answered the obvious, but answered my questions ^^ (BTW, what if that javascript file comes to be used in just ONE page? Shouldn't it be better to include it on the same file instead of making a new HTTP request? since having that cached is totally worthless, actually having any further use) – Diego T. Yamaguchi Jul 01 '13 at 03:20
    • In general linking is a better approach. I'll add an addendum to the answer –  Jul 01 '13 at 03:26
    0

    I would consider something like this

    <script src="js/js.php"></script>
    

    where js.php includes all the need js files I assume this will resolve the caching issue, plus you can make things dynamic by adding get values I guess.

    btw I find it better to use the php open and close tags for html whenever possible

       <script src="<?php echo $var ?>" ></script>
    
    phpdev76
    • 45
    • 4
    • Your assumption that this will "resolve the caching issue" is incorrect. Each time js.php is fetched, the server will parse that PHP and the result will not be cached. JavaScript goes in .js files, PHP goes in PHP files. I also can't think of many situations where it would be useful to have a php $var pointing to a static file, although this will not inherently nullify the caching benefits as your first suggestion will. Since I'm in stickler mode, your PHP echo lacks a semicolon and will cause a parse error. – Matteo C Jan 06 '16 at 01:35
    -1

    As I commented before, <script src="js/script.js"></script>.

    This is in you <head> and it will be implemented before anything goes into you <body>

    Since you are building your front end via JavaScript, php functionality will come after everything was built by JS.

    Brian
    • 4,958
    • 8
    • 40
    • 56
    • I thought it was a recommended practice to put – Diego T. Yamaguchi Jul 01 '13 at 02:51
    • And I still ask: WHY? Why this is better than the other one? – Diego T. Yamaguchi Jul 01 '13 at 02:52
    • Well, if you are using extjs, you can do Ext.loader('jsname') – Brian Jul 01 '13 at 02:52
    • sry my ignorance, but now I need to search about this extjs that I don't know about (thanks for answering) – Diego T. Yamaguchi Jul 01 '13 at 02:54
    • 1
      its a JS library, but do not worry about it now. If you are not using it, you won't need it anyways. Its best not to mix php and JS together. Keep them separate – Brian Jul 01 '13 at 02:58
    • Why should I keep them separate? It's not like they will explode if I put them together, like, i usually use some php variable to echo a javascript line without major issues – Diego T. Yamaguchi Jul 01 '13 at 03:01
    • 1
      it is not an issue, just not the best programming practice. It is bad for readability and later maintainability – Brian Jul 01 '13 at 03:03
    • but I don't think that i'll avoid to do something that is totally necessary (MUST DO) because of readability and maintainability. Actually, this thing about readability is just a superficial issue, instead, the major concern should be that you made things works. (in other words, this is not about best programming practice) – Diego T. Yamaguchi Jul 01 '13 at 03:06
    -2

    Well according to me using the later approach is better , if you are designing a php page it is always better to write everything in php , whereas HTML side scripting is better done inside echo"" or print""; functions , you can read a lot about it in w3schools.com , hope my answer solved your problem.

    Divyanshu Negi
    • 592
    • 1
    • 5
    • 24
    • 1
      first: why everything inside echo? second: w3schools.com? whatahell ò.ó (though, thanks for the answering, I also think that the second is better) – Diego T. Yamaguchi Jul 01 '13 at 02:48
    • I have to agree with pk7. There is absolutely no call or need for static markup to be spat out via echo/print. It makes it very difficult to read and understand the structure of the document. However, if you need to generate markup based on a calculated result -- like adding bold text or strikethrough depending on variable values -- then you should use either echo/print or short open tag notation (=$foo?>). – Skudd Jul 01 '13 at 03:04