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.
Tracy said:
On 12 Nov 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)
Stephanie said:
On 12 Nov at 7:44 pm
THANK YOU THANK YOU THANK YOUUUUU.
Stephanie said:
On 12 Nov 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. :(
Jem said:
On 12 Nov at 8:28 pm
They’re now in the main body of text. I’m still fiddling though…
Stephanie said:
On 12 Nov at 8:37 pm
@Jem: Still going for “more bloggy”?
Josh said:
On 12 Nov at 8:38 pm
Rasmus Lerdorf? Isn’t that a type of salad?
Carly said:
On 12 Nov 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?
Jem said:
On 12 Nov at 8:44 pm
TBH, if your file works as intended, then you’ve done it right ;)
Joe said:
On 12 Nov 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
Roxanne said:
On 12 Nov 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.
Vera said:
On 12 Nov 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
Robert said:
On 12 Nov at 9:08 pm
Similar to how tables aren’t meant for layouts. Ah, where would we be if no one used those?
Clem said:
On 12 Nov at 9:09 pm
But Jem, I use PHP includes so I KNOW PHP. DUH.
M said:
On 12 Nov at 9:38 pm
I WANT VERDANA BACK :'(
Jacky said:
On 12 Nov 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!
Jem said:
On 12 Nov at 10:37 pm
Yeah, I’m not so sure about the Arial yet myself…
Stephanie said:
On 12 Nov 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!
Liz said:
On 12 Nov 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
Vasili said:
On 12 Nov 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.
Louise said:
On 13 Nov at 12:57 am
?php echo “hello world”; ?
Yay I know php!
Audrey said:
On 13 Nov 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.
Hannah said:
On 13 Nov at 2:46 am
Amen to that. :P
Melissa said:
On 13 Nov 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.
Adrianne said:
On 13 Nov 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…
Anita said:
On 13 Nov at 7:32 am
I hate it when people have tutorials on “How to use PHP” and it only shows dumb stuff about includes :(
Julia said:
On 13 Nov 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.
Julie said:
On 13 Nov 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.
Jem said:
On 13 Nov at 11:48 am
Nah, this is pretty common Julie.
Julie said:
On 13 Nov 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…
Jem said:
On 13 Nov at 12:09 pm
Yep! The tutorials still suck…
Mike Haddad said:
On 13 Nov 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.
Jamie said:
On 13 Nov at 7:18 pm
lol i tried to learn php so i know it when i get hosted but a failed :P
Amber said:
On 13 Nov at 8:19 pm
Use the new layout IMO
Mumblies said:
On 13 Nov at 8:42 pm
Please don’t reverse it, that hurts my eyes…too much white. I much prefer this layout.
Vanesa said:
On 13 Nov at 8:56 pm
THANK YOU for writing this!
Carly said:
On 13 Nov at 9:19 pm
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…?
Zatanna said:
On 13 Nov 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.
Mimi said:
On 14 Nov 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.
… huh?
Lewis Walsh said:
On 15 Nov 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.