Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics PHP on the Web HTML and PHP

Why isn't PHP code a separate file that's referenced in your HTML?

In some of the other languages that I've looked at so far, like JavaScript, you reference the JavaScript inside your HTML vs. placing all your JavaScript code inside your HTML file. I understand that PHP be interpreted by the server as HTML and will return HTML from the server, but isn't it bad practice to write PHP inside your HTML file?

Or maybe a better way of phrasing the question is why don't we keep the PHP code separate from the other types of code that we work with for better readability and a more logical structure? (like we keep CSS and JavaScript separate from the HTML files)

5 Answers

There are several different questions/issues here.

1) as the server/browser relationship goes, you cannot include php in html, since one is server side and one is client side.

2) in practice we DO separate view code from controller code (google Model-View-Controller). However, I think for the purposes of learning, it's better to see the bits of code side by side so that you can visually understand whats happening. even so, it's nothing more than a aesthetic issue.

3) regardless of how you try to separate the two (unless you're using a client-side program and calling information from an api) your html is going to have to get processed through php if you're using any type data from the php, since html can't use a php variable.

the short answer is, css and js are both client-side and can be loaded at runtime, so they can be included in html. php cannot.

Bryan Peters
Bryan Peters
11,996 Points

The server processes the PHP before it ever gets to the client, so the client browser only gets the resulting HTML without PHP logic details included. JavaScript included in the way you mentioned is sent with the HTML to the client for interpretation. JavaScript minification can help obfuscate your code to some degree, but generally your logic and some backend details are exposed via JavaScript. With PHP, you can keep much of this hidden from end users. The key is finding the right balance performance and security-wise as to what logic to do client-side via JavaScript, and what to do server-side with PHP. Also, you can still modularize your PHP with includes.

This course doesn't get into the separation of concerns, however there is a course by Hampton Paulk that covers a more advanced example - https://teamtreehouse.com/library/building-websites-with-php.

Tyler Durden
Tyler Durden
2,406 Points

you CAN write php in a seperate file and reference inside a HTML file, how did no one respond to you earlier and explain this?

it's called 'INCLUDE' so you would write:

<? php
include_once 'YourFileName.php';
?>

and you put that one the top of your HTML file like index.html. It comes in super useful, for example, if you have a <nav> tag with a menu list inside it, like for home, blog, contact ETC... on like 10+ pages on your website. Say one day , you want to add a page called "about me", you would normally have to go through the 10+ pages on your website and edit it manually. With the "INCLUDE" php code, you can copy the entire <nav>ALL OF ITS CONTENTS</nav> and put it in a separate PHP file and THEN reference it on every HTML/PHP of your site by the <? php include 'YourFileName.php'; ?>

Al, because the OP specifically asked about HTML files, your answer is misleading. A server, by default, will not process php tags inside of an html file. If you write

<? php
include_once 'YourFileName.php';
?>

inside an html file, it will just print that code to the screen. You can "include" php files inside of another php file, but not an html file.

Note that you can tell your server to interpret html files as php files, or even use phtml, but that's not standard. And for the purposes of the scope of this question, it's not really relevant.

Tyler Durden
Tyler Durden
2,406 Points

OK, I misunderstood and thought the OP was wanting a way to include PHP code into his main .html file (or renamed .php when running php inside it) without writing the <?php code blocks inside of it, and linking it like you do with JavaScript.