Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
2  
The less php you have in your php, the better. –  technosaurus Oct 9 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 at 19:00
2  
This question appears to be off-topic because there is no code to review –  AD7six Oct 9 at 20: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 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 –  technosaurus Oct 9 at 22:09
show 1 more comments

migrated from codereview.stackexchange.com 2 days ago

This question came from our site for peer programmer code reviews.

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
 
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 at 19:00
 
I improved the answer with a performance consideration –  Federico Oct 9 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 2 days ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.