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

HTML

HTML VS PHP

Is it better to code a site in php or html? What I mean is the file extensions? Saving the files as html or php. What are the pro's and con's either way?

2 Answers

If the file contains PHP code, you have to use .php, if it's only plain html use .html

PHP creates HTML dynamically. It can be pretty useful. Take for example the footer of a page

<p>2013 All rights reserved</p>

You can make the date to update automatically with PHP, so you don't need to come at the end of the year and change every single page manually. In PHP this same code looks like this:

<p><?php echo date('Y'); ?> All rights reserved</p>

The file needs to have a .php extension. The server process the PHP file and return a HTML file with all the data requested dynamically in its place. The benefit is that the date is evaluated at run-time so the user always see the current year.

PHP also can make your life easier. You can create "blocks" of HTML and mix them in one page. For example, you can create a header.php and a footer.php files. In the INDEX you know that you want to include both files so you write something like this:

<?php include('header.php'); ?>
<!-- Your HTML content -->
<?php include('footer.php'); ?>

The result page that the end user will see should look like:

<!-- The HTML content that is inside header.php -->
<!-- Your HTML content -->
<!-- The HTML content that is inside footer.php -->

The benefits of working with this modular blocks are huge! If for some reason you need to modify the header of your site, you only need to change the code inside header.php and all the pages that call this "block" will update properly.

I don't know if I was clear enough but PHP has many advantages compared to plain HTML. You should look the videos here at Treehouse.com - they explain this a little bit better. Take a look: http://teamtreehouse.com/library/programming/build-a-simple-php-application