Wednesday, 27 January 2010

Including Files in HTML using PHP

Not interested in the waffle, then page down to Links paragraph at bottom

WHY PHP (waffle)

Creating a web site using native tools has advantages and disadvantages, but I have decided to see how practical it is.

One disadvantage is common code, say a menus system on the top of each page, could be in multiple pages, perhaps 30 or so. If this needs to be changed, say you add a new section to the site that needs to be addressed by the menu system, all these have to be updated. As well as being tedious this can also lead to inconsistencies, similar to redundant data in data bases.

This seems obvious to me, but there appears to be no HTML command eqivalent to an include in programming languages. Even more so considering that CSS existence is based upon this approach, albeit using a different method.

The solution may be PHP (PHP- Hypertext Preprocessor) not sure what first P stands for!

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. It is open source project and works on most Apche servers, included by the free web hosting service I use, Zymic.

PHP does masses of things, neatly all of which I have never used or understand, but including HTML code from one file into a "master" HTML file is easy. Easy once you realise that the name of the files have to have extension .php and  not .htm. The PHP changes the final "built" file to .htm before it presents it to the browser.

This is fine except I had compatibility errors reported by W3C validator when XML header was present, and no way I thought at first, of previewing results before uploading server and accessing file as a normal user.

After a couple of hours I realised you can overcome problem by not including the first line command that declares xml such as

?xml version="1.0" encoding="UTF-8"?

however this is probably a retrograde step.

So I decided not to use PHP and deciding to use HTMLKit as my editor I started  learning how to drive the editor, and there on the first page was reference on how to install PHP Preprocessor on a Windows PC.

Really easy to install. I guessed I would need to also install a plugin into HTMLKit before it started to work, but not so. Once PHP installed I opened two PHP files I had built earlier, previewed them with editor preview option and they worked as if native HTML and same again when previewed in browser. So now the second objection has been removed I will proceed with the PHP


Here are relevant links for download and more information.

PHP  Main PHP website

How to install PHP on the local computer Page from HTMLKit support that shows links to relevant download sites etc.

HTMLKit Note HTMLKit 292 is the free version, HTMLKit Tools is not free.

No comments:

Post a Comment