Tuesday, December 23, 2008

«Learning PHP»

PHP LogoYesterday I spent the day keeping myself busy with my blog supplement pages. I'm finally taking the time to learn php. I'm re-writing all my non-blog pages in php. The great thing about that is if I want to change something in the sidebar or whatever, I don't have to go through and make that same change over and over in the html code for each individual page. I'll change it once in the main php code, and it will take effect on every page.

The actual blog is hosted by Blogger, and thus will be unaffected by all of this.

Basically, I'm using the require() function to paste in all the code that is the same on every page; things like the sidebar and footer. It reduced the actual .html files for the web pages down to the content that is unique to that page. As an example, my template page that I use to create new pages was 200 lines (before actual content is added), now it is 31 lines.

None of the web addresses or file names are going to change, though. You'll notice they still have their .html extension. In fact, the files themselves are still .html files. I just told it to run .html as a php file with a little .htaccess magic:

AddType application/x-httpd-php .php .htm .html
AddHandler x-httpd-php .php .htm .html

Also, the non-www version of my web address finally 301 redirects to, as I've wanted it to all along.

I'm sure there's so many neat things I could do with php, I'm just beginning.


  1. I use the same approach on one of my sites and essentially what you end up with is your own template for your web pages with the major elements ( eg header sections, sideebare sections) pulled through as server side includes.
    I have used the include function for this and thought this was better because errors dont stop script execution. However on reflection I think you may be right in using require as perhaps an error should stop execution - until it gets fixed. Any thoughts?

  2. @ Bunc: Yeah, that's what I figured after I read about the difference between the two. If there's an error, whatever comes after it is going to be corrupt and not display right anyway. No need to waste any more resources and CPU cycles on it.

  3. I think that your certainly right when it comes to "scripted" elements of a website. Any error could have repercussions further in the script and it's probably better to stop this happening. I am not so decided about server side includes though where this is simply being used to , for example, include a header or sidebar element which might largely be text or images etc.

    In such a situation I might want the page to continue loading with the rest of the elements whihc are not causing an error. At least the visitor would see the remainder of the page.

    It's probably academic though in that kind of situation because errors are unikely where the include is only of standard text, links etc etc.

    Require is defo better for including script elements but I am undecided for more bog standard includes. On balance I think I might change to using require though.

  4. @ Bunc: Well, how I have mine set up for example, if the first script were to fail to load there would be no html header and the browser wouldn't even know what type of file is being served to it.

    Also, the html below would be malformed. It would chose the wrong ending tags for very important div tags. Nothing would match up.


Thanks for taking the time to comment.

Note: Only a member of this blog may post a comment.

»» «« »Home«