Skip Site Navigation «

Law of Closure

vnav«labs«PHP«closure
Closure

a Gestalt principle of organization holding that there is an innate tendency to perceive incomplete objects as complete and to close or fill gaps and to perceive asymmetric stimuli as symmetric.

Law of Closure — 2003, Princeton University

Humans tend to enclose a space by completing a contour and ignoring gaps in the figure.

Visual Communication, Ron Allman

You’re terminated.

Sarah Connor (to the Terminator) — 1984, MGM Studios

Contents

  1. Introduction
  2. The footer() Function
  3. Hello World
  4. Additional Resources
  5. PHP Labs Navigation

Introduction

It wasn’t so long ago that I would think why bother with a closing </html> tag? it’s just HTML after all, and the browser doesn’t care. I don’t do that anymore. I used to omit most of the attribute quotes too, which I miss and it sure keeps file sizes down. But I don’t worry about that too much now that I have tools like mod_gzip. If you’re not using it you should give it a try. My results are showing that outgoing XHTML is reduced by around 70% (which saves you on bandwidth costs) and the pages are rendered much faster on the client-side.

But this mindset originated with the lazy style of markup inherent in older versions of HTML. And also from programming in languages like Perl where: falling off the end of your program is just a normal way to exit (Larry Wall, Programming Perl, 3rd Edition, page 3 — see the about page for the cited source). In other words you’re not required to call exit() to end your script, the interpreter assumes that’s what you want to do when the source code ends. PHP is the same way, the only time I ever terminate a script using exit() is in the case of an error condition when it’s necessary to end execution of the program prematurely.

XHTML is an XML application language folks, end tags are required. I've adjusted my style of writing markup to (accurately) think of tags as markers for blocks of related statements. PHP, Perl, Javascript and C (et al.) use the same syntax for this, an opening and closing brace character:
{ ... }

Here are examples of each that are so similar it’s almost frightening:

1. <html> <head> <title>My Document</title> </head> <body> <p>My content... </body> </html> XML Parsing Error: mismatched tag. Expected: </p>. Location: oops.xhtml Line Number 7, Column 4: </body> ---^ 2. <?php if (true) { echo "And the truth shall be known.\n"; exit; else { echo "Beware of false prophets.\n"; exit; } ?> [PHP] Parse error: unexpected T_ELSE in oops.php on line 6

The fix for the first example is as simple as closing the paragraph with a </p> tag, the second is equally trivial: close the if() block with a ‘}’.

The XHTML returns: My content...
The PHP returns: And the truth shall be known.

I've heard comments from some developers stating they find these XML error messages ugly or bothersome. Excuse me? The first thing any programmer checks is to make sure their code at the very least parses correctly (is syntactically valid). Raise your hand if you’ve ever spent hours wondering why your page doesn’t render correctly because you forgot to close some tag and the BROWSER NEVER COMPLAINED. I rest my case.

In the <body> Element lab I eluded to this mystery fourth function in my body.php library module. There’s nothing mysterious about it at all, here’s an abridged version:

1 </div> 2 <div id="footer">$footer</div> 3 <div id="footbar"></div> 4 </div> 5 </body> 6 </html>
  1. close the #content </div>
  2. display the #footer
  3. and #footbar
  4. close the #canvas </div>
  5. close the </body> element
  6. close the </html> element

And there’s your closure. Let’s have a final look at our minimal XHTML document framework:

Content-type: application/xhtml+xml; charset=UTF-8 <?xml declaration?> <!DOCTYPE declaration> <html> <head> <title>My Document</title> ... </head> <body> <p>My content...</p> </body> </html>

Hello World?

I don’t think so. I also searched this site for the terms foo and bar and found nothing. So I guess you won’t find any such metasyntactic nonsense around here. I did find a few footers and even a footbar though (see above). What you can find is a complete sample document on the page. It should demonstrate pretty much everything I've discussed in this series in one neat little package.

Additional Resources

  1. HTML
  2. HTTP
  3. MySQL
  4. PHP
  5. XHTML

Questions? Comments? Hate my guts? You can Contact Me via email, or post a message to the Discussion Forum.

Last updated: Monday, October 20th, 2008 @ 2:23 PM EDT [2008-10-20T18:23:29Z]   home

(c) 2010-2012, Douglas W. Clifton, loadaveragezero.com, all rights reserved.