Hey guys and gals, I’m curious about something and I would first like to say that I’m sorry if this isn’t the correct forum to post in, but seeing as though it’s a PHP forum I thought I’d ask here.
I have used many CMS, forums, albums and other such scripts over the years and I’ve noticed (from memory) that the index.php does not contain any or very little HTML. Why is that?
I’ve used several tutorials and books over the years when I urge to learn web development but I’m yet to find anything that explains this method. My only assumption is that it has something to do with themes or a certain layout of the script?
I’d appreciate anybody shining some light on this for me.
Well I use WordPress as my CMS of choice and when I check out the index.php file, I find hardly any HTML markup in there as you said.
The reason for this is because PHP is such a powerful language, especially when the developers of WordPress write it!
Just a simple command like;
< ?php the_content(); ?>
Will grab all of the content in a page and display it on the screen. Obviously this function has more lines of code behind it that does all of the hard work and it’s outputted as a function in this tiny little line.
However, when you’re dealing with static websites, they contain little or no PHP at all as it is just raw HTML such as h1 tags, p tags, ul’s, li’s, div’s etc… and this is just hard-coded into the page.
So what is the purpose of using that statement? Where is the actual HTML used?
I understand ‘how’ this works, I just don’t understand why. Where could I learn more about using this type of method? Sorry for all the questions, it has just been something that has confuzzled me in the past!
This was a huge thing for me when I started to work in PHP over static HTML – the index page will more than likely contain includes to stitch together the whole page.
A good example is the document header, with your css link, meta tags, scripts etc. All you need to one PHP document with this in, then include it in your pages in place of the header itself.
My index pages are usually just a handful of includes – one for options (connection strings, common variables etc) document header, site body, main/sub menus, Google Analytics code, etc. These pages will have a mixture of PHP and HTML.
I have yet to move on to OOPHP, so take my post as being a way of doing things when working with procedral PHP.
As for the “why,” you might be interested in what I describe as [php-first](http://dynamicdrive.com/forums/entry.php?267). Even if you don’t use a full-on MVC pattern, you can avoid a lot of problems in your PHP (and write more efficient code) if you simply make sure that **output** (HTML, etc., anything that goes to the browser) is the *last* part of your script.