I have been writing my website, front-end: html, JS, jquery and css, back-end: php mysql. However my php files only contain php and mysql code. Is this a really bad thing? In what situation should PHP files contain html/javascript?? Currently my php files are just there to receive request and response with array of information. I just dont understand why people would put front-end stuff in back-end, is it for security?

  • 2
    The less php you have in your php, the better. Oct 9, 2013 at 18:58
  • 2
    please provide some code to review. or at least some examples of what you are doing and why you ask the question about the segregation between Front-end/Back-end. really there is no segregation because the back end should have some logic for serving some information to the front-end
    – Malachi
    Oct 9, 2013 at 19:00
  • @AD7six, Malachi Sorry I don't know where else I can post this question. Doesn't seem to fit stackoverflow. The reason why I am asking this question is, because I m learning PHP frameworks like Yii, that uses MVC model. But from my understanding 'view' would be the html codes, which makes me wonder if I have been doing thing wrong with my php. I am a new to web development in case you didnt already realize
    – user1294510
    Oct 9, 2013 at 20:45
  • That is why you send data to js and let it take care of any css, html stuff and as much processing as you can do client side Oct 9, 2013 at 22:09
  • Even with traditional non-ajax sites I prefer to separate code that actually does something from the markup. In php that could be smarty templates, or simply seperate php files. Oct 12, 2013 at 11:02

2 Answers 2


I think that it a good practices to separate front-end to back-end.

I am supposing that you are using AJAX to communicate with the back-end, and it is the reason why you are not using HTML/JavaScript in your PHP files. If you have to create a dynamic web page without AJAX, you have to write some HTML code in your PHP file.

Doing every front-end operations with JavaScript imply that these operations are done by the browser. If you prepare the HTML on the server side, the browser just render the code without extra effort.

  • yes, I am using ajax, so there is no benefit in writing html/javascript code in php? I see people do it all the time, I thought there might be a benefit of some sort. Thanks
    – user1294510
    Oct 9, 2013 at 19:00
  • I improved the answer with a performance consideration
    – Federico
    Oct 9, 2013 at 19:06
  • 2
    @user: The benefit is in making things easy for lazy/inexperienced developers working on small projects, mostly. There's less overhead to do it that way, you just happen to write something utterly unmaintainable if it's a non-trivial website.
    – Phoshi
    Oct 11, 2013 at 15:48

Three questoins, three answers. YMMV.

1. Is this a really bad thing? (that you have no HTML or javascript in your PHP)

No, it isn't. It's just fine to have clear separation of concerns, especially for PHP files meant to be included by other PHP files, or core navigation PHP's.

What would be bad form is if you're not including any HTML, XML, or JSON in the final responses your PHP code returns.

2. In what situation should PHP files contain html/javascript?

PHP files should include client-side language to the extent that separating out such language into their own files does not provide a reduction in labor or complexity sufficient to offset the cost of the separation itself.

For example, it's perfectly acceptable to do a block of code like the following, to parrot back the fields of a HTTP POST request as hidden <input> fields:

?><input type="hidden" value="<?php $POST('email') ?>"> <?

3. I just don't understand why people would put front-end stuff in back-end, is it for security?

PHP is not the back-end. The database server that your web-server connects to is the "back-end". PHP is a front-end language designed to dynamically build HTTP responses, most of which by size and volume are HTML documents.

  • I would say that nowadays frontend means literally code that a user receives i.e. html, js, css. A frontend developer may/typically know only about these technologies. OR: Building html pages with php does not make php a frontend language, just as serving a webpage with apache doesn't make apache configuration a frontend technology =)
    – AD7six
    Nov 5, 2013 at 16:53
  • Ahh, semantics. What you call "front-end" i'd call "client" or "remote." Unless we were talking about some device where we had more control, like a HMTL-based smartphone app. :D
    – DougM
    Nov 5, 2013 at 20:49
  • Look at the kind of skills require for frontend jobs - where listed, a server-side language is typically a bonus and almost informational (I use jobs merely as a supportive reference). For example: If you only know php I would not advise describing oneself as a frontend developer :)
    – AD7six
    Nov 5, 2013 at 20:55
  • Lol. If you only know PHP, I'm not sure you can describe yourself as a "developer", period.
    – DougM
    Nov 5, 2013 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.