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!

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 (Retired) What is PHP? Intro to Workspaces and PHP Code

I'm not understanding why PHP doesn't work in tandem with an HTML document...in other words, why don't we need both?

Just diving into the PHP basics, and I've already hit a question on the first video. Hampton really never explained why we simply replace the HTML file with PHP. This doesn't make sense to me after seeing the needed relationship between HTML/CSS/JavaScript on other web pages. You would never just rename and replace your HTML file as a CSS file for example... I thought PHP would have the same relationship that HMTL/CSS/JS do, that you need HTML for some things and PHP for others...so why the heck is Hampton simply replacing the HTML document with PHP then?

1 Answer

Mark Wade
Mark Wade
13,112 Points

If you mean the file extension - using ".php" indicates to the server that the file is PHP and HTML. Until you open the php tag, it's treated like a normal HTML file. If you try to put PHP code in a ".html" file, depending on how the server is set up, it might not work.

If you mean why would you have a document with only PHP and no HTML - while often you will insert PHP into a HTML document in the way you're familiar with, PHP does need to be used in conjunction with HTML. For example, you could write a PHP program to send an email and not display anything - in this situation, you wouldn't need any HTML around it. More likely though is that as you develop more complex PHP applications (like Wordpress, Facebook, etc.) you'll want to start with PHP as the entry point and then have it use a tempting engine to decide what HTML and other resources to load. For now though, just think of it as you're just focussing on the PHP part.

Sorry I didn't clarify fully, in his video he replaces index.html to index.php so your first answer fits what I was looking for more, but I appreciate the other possible explanation.

That is an interesting concept that I don't fully understand in terms of how a PHP file could be seen as both HTML and PHP by the program's interpreter. So is the file read as HTML on the client side and on the server side it's read as PHP?

Mark Wade
Mark Wade
13,112 Points

The server will compile and evaluate everything in the php tags before sending whatever the output is to the browser for it to render. Depending on your server set up, the .php extension may be needed to tell it that it needs to look for php tags in it before just sending to the browser, or it may not - file extensions aren't strict in that a file with whatever extension has to contain what it says. In fact, you could even have a file called "image.jpg" and in that file put PHP code to generate an image (I've done this myself to have rotating avatars on forums) - provided you tell the server in some way (usually a ".htaccess" file if it's an Apache server) that that's what you're doing.

But, generally speaking: put PHP and HTML in a file ending in ".php" and think of it as "PHP and/or HTML".

Here's the explanation from the PHP manual: http://php.net/manual/en/language.basic-syntax.phpmode.php