Nov 12 2008

PHP Includes Aren’t What You Think

Newsflash: knowing how to do implement PHP include() doesn’t mean you “know” PHP, sorry and all that. Yes indeed, I hate to be the bearer of bad news, but this cool “layout technique” you’ve learned isn’t a layout technique at all. It has nothing to do with layouts. In fact, PHP and design are two entirely different subjects altogether.

The PHP function (well, statement really) include() is a means by which to … wait for it, you’d never guess this one: include one file in to another. That means that yes, when you put your meta tags/etc in to header.php and reference it using <?php include('header.php'); ?>, you’re using it right. However, no, this was not what Mr Rasmus Lerdorf had in mind when he implemented the bloody thing. (No, I have no facts to back this up. Shut up.)

Funnily enough, if you were to create a file called jem-has-big-boobies.php and use: <?php include('jem-has-big-boobies.php'); ?>, that would work too! The filenames header.php and footer.php aren’t a magical tool by which you tap in to some secret PHP functionality to make your website really l33t, honest.

Don’t get me wrong, I’m not advocating you all go out and start creating jem-has-big-boobies.php includes — as fucking hilarious as that would be — and I’m not suggesting that you’re doing something wrong by using the functionality of PHP’s include(). I am however begging those of you who don’t understand why or how it works to spend a little time with my friend the PHP manual. It doesn’t bite, I promise.

Warning

This post is over 6 months old. This means that, despite my best intentions, it may no longer be accurate. Age, motherhood, experience, loss... these things have all changed me from when this blog was started back in the heady (ha) days of my youth.

As much as I would like to go back and edit 10 years of archives to provide an insight into the 'me' of now — to update coding snippets and revise website advice — it would probably take years to do so (by which point I'd have to start again!) This would defeat the point of keeping these archives anyway.

Please take these posts for what they are: a brief look into my past, my history, my journey.

40 Responses so far
  1. Tracy says: November 12, 2008 at 7:43 pm # ·

    *thinks about big boobs* … you were saying?

    Anyways, yes, PHP. It’s not THAT easy. Once I’m done with my studies I need to concentrate on learning it. What I’ve learned in uni isn’t enough. I was proud that I could create my navigation though AND add links from my DB to my site :) (well… with a little help from you)

  2. Stephanie says: November 12, 2008 at 7:44 pm # ·

    THANK YOU THANK YOU THANK YOUUUUU.

  3. Stephanie says: November 12, 2008 at 8:05 pm # ·

    What, really? And all this time I thought I knew PHP. I’m so going to include a jem-has-big-boobies.php file in my WP template somewhere now that I know that that’s what PHP really is. ;)

    Also, your asides are gone. :(

  4. Jem says: November 12, 2008 at 8:28 pm # ·

    Also, your asides are gone. :(

    They’re now in the main body of text. I’m still fiddling though…

  5. Stephanie says: November 12, 2008 at 8:37 pm # ·

    @Jem: Still going for “more bloggy”?

  6. Josh says: November 12, 2008 at 8:38 pm # ·

    Rasmus Lerdorf? Isn’t that a type of salad?

  7. Carly says: November 12, 2008 at 8:43 pm # ·

    I was looking through all the includes in the websites at work – thought they all use the header / footer / menu etc (we use a load more too) they all also seem to written in different ways. Some use DOCUMENT SERVER ROOT or something before the path, some have these: ‘ some use none of those, and some have ( and some don’t. Is there one correct way of doing it?

  8. Jem says: November 12, 2008 at 8:44 pm # ·

    Is there one correct way of doing it?

    TBH, if your file works as intended, then you’ve done it right ;)

  9. Joe says: November 12, 2008 at 8:53 pm # ·

    Half the 12-year-olds who do this probably don’t even realise that PHP is something completely separate from HTML.

    The idea that they can actually do more than just split their files up into headers probably hasn’t occurred to them either.

    It’s probably good that they’re none the wiser: If they learned what can actually be done with PHP, incredibly shit and insecure scripts are going to start popping up everywhere. :P

  10. Roxanne says: November 12, 2008 at 8:59 pm # ·

    Whoa, I thought this was common knowledge! Guess not…or that could be me just being ignorant… =P

    Side-Note: I see your editing the look of your blog. I actually liked the comments links underneath the blog title…easier to find! Personal opinion of course.

  11. Vera says: November 12, 2008 at 9:02 pm # ·

    … but but but I was CERTAIN that my layout was so 1337 now… *chucks entire site away in a huff*
    Meh, for a beginner the PHP manual can be quite a “mouthful” though. But then again, coding is not for everyone, or else everyone would be a programmer XP

  12. Robert says: November 12, 2008 at 9:08 pm # ·

    Similar to how tables aren’t meant for layouts. Ah, where would we be if no one used those?

  13. Clem says: November 12, 2008 at 9:09 pm # ·

    But Jem, I use PHP includes so I KNOW PHP. DUH.

  14. M says: November 12, 2008 at 9:38 pm # ·

    I WANT VERDANA BACK :’(

  15. Jacky says: November 12, 2008 at 9:58 pm # ·

    Really? People don’t get this? I got this immediately after I implemented them into my site, way back on Mirkwood Wench! :P

    But I think a “Jem-has-big-boobies” revolution would do nothing but help the web community. ¬_¬; Heh!

  16. Jem says: November 12, 2008 at 10:37 pm # ·

    I WANT VERDANA BACK :’(

    Yeah, I’m not so sure about the Arial yet myself…

  17. Stephanie says: November 12, 2008 at 10:56 pm # ·

    Whoa, the asides are now in the main body of the text. (Yes, I see your comment.) Okay, they’re not gone. That’s helpful. But, well, I never did like seeing them in the main body… for some reason that just confuses me. Personal preference. I did use your tutorial on how to do that to implement my own ‘asides’, so thanks for that!

  18. Liz says: November 12, 2008 at 11:04 pm # ·

    Sadly I actually have seen php includes as a tutorial on very “trendy” sites. lAWLZZ new layout technique instead of iframes! Ahaa I should name my header Jem-has-big-boobies.php

  19. Vasili says: November 12, 2008 at 11:42 pm # ·

    I was going to write an article like this… IF MY SITE WORKED FOR ME. >:( I was scared of the PHP manual in the beginning, but now I can’t live without it.

    I don’t like the asides plus posts mixed together; the padding confuses me.

  20. Louise says: November 13, 2008 at 12:57 am # ·

    ?php echo “hello world”; ?

    Yay I know php!

  21. Audrey says: November 13, 2008 at 1:22 am # ·

    PHP is a coding language and HTML technically isn’t. That’s the distinction, I think, some people don’t understand.

  22. Hannah says: November 13, 2008 at 2:46 am # ·

    Amen to that. :P

  23. Melissa says: November 13, 2008 at 4:51 am # ·

    Haha! So true. I’ve been using includes for ages and I adore them, but I would never go around saying I’m a PHP ninja! I’m just pretty happy if my sites load from day to day without errors.

  24. Adrianne says: November 13, 2008 at 5:21 am # ·

    LOL. Don’t forget those who download a PHP script from some site (yours, for example, Jem), fill in the fields where the comments say “change this to (this)”, implement it on their sites and then claim to the world they know PHP. (sadly I’ve run across sites in which the owners actually claim this…)

    Although you gotta admit that the PHP’s include() is heaps better than HTML frames. Even until now I still see some sites who still use frames. Hm…

  25. Anita says: November 13, 2008 at 7:32 am # ·

    I hate it when people have tutorials on “How to use PHP” and it only shows dumb stuff about includes :(

  26. Julia says: November 13, 2008 at 10:17 am # ·

    I’ve always known that includes isn’t PHP altogether. Its a statement that is used to include files.

    Nice name for the example file. :P You can call the file whatever you want & it will include itself.

    I’ll try reading your PHP manual & see if I can understand PHP more.

  27. Julie says: November 13, 2008 at 11:34 am # ·

    Did you have a specific person in mind? It’s late (or early, I’m not sure which by now) and I want some entertaining dramuh, which is hard to cause without some good ol’ finger pointing.

  28. Jem says: November 13, 2008 at 11:48 am # ·

    Nah, this is pretty common Julie.

  29. Julie says: November 13, 2008 at 12:07 pm # ·

    I know, but I was still hoping fun would ensue.

    BTW, have you been to pixelfx lately? Haven’t looked at the tutorials yet, but there finally has a new layout up…

  30. Jem says: November 13, 2008 at 12:09 pm # ·

    Yep! The tutorials still suck…

  31. Mike Haddad says: November 13, 2008 at 4:27 pm # ·

    Been a long time since I’ve had a chance to use PHP (I’ve been stuck with Java in school for the last little bit, and it’s eaten up all of my time), but I seem to remember include() not being limited to just php either. That might melt some brains.

  32. Jamie says: November 13, 2008 at 7:18 pm # ·

    lol i tried to learn php so i know it when i get hosted but a failed :P

  33. Amber says: November 13, 2008 at 8:19 pm # ·

    Use the new layout IMO

  34. Mumblies says: November 13, 2008 at 8:42 pm # ·

    Please don’t reverse it, that hurts my eyes…too much white. I much prefer this layout.

  35. Vanesa says: November 13, 2008 at 8:56 pm # ·

    THANK YOU for writing this!

  36. Carly says: November 13, 2008 at 9:19 pm # ·

    BTW, have you been to pixelfx lately? Haven’t looked at the tutorials yet, but there finally has a new layout up…

    It makes me laugh it’s a tutorial site, using a premade layout. Also the text is hard to read, dunno if it’s cus of the mac – or if everyone thinks it…?

  37. Zatanna says: November 13, 2008 at 11:54 pm # ·

    (No, I have no facts to back this up. Shut up.)
    *giggles*

    I never thought that I knew PHP when I first discovered PHP includes. I was just relieved that I didn’t have to edit every page of my web site to add/take out something. Plus I’d take one look at (any) PHP script and know that’s what PHP really was.

  38. Mimi says: November 14, 2008 at 7:03 am # ·

    I was actually explaining includes to someone today and I referred them to your tutorial on Tutorialtastic. I believe you mention something about this near the bottom of the page, no? Not quite in the same manner but.. ah! This was entertaining to read nonethless.

    lol i tried to learn php so i know it when i get hosted but a failed

    … huh?

  39. Lewis Walsh says: November 15, 2008 at 11:31 pm # ·

    Just an FYI:

    If the file you are ‘including’ can not be found then the PHP script will continue to run without it, though you will most likely see a warning report.

    If you use require() or require_once() instead of include() then your PHP script will halt if the required file can not be found. This can save a lot of problems if database queries are executed based on data from an include() that couldn’t be found.