Pretty Hip PHP

I’m going to digress for one week from the usual fare in order to talk a bit about PHP, which I just discovered recently and which I’m already crazy about.

PHP is a scripting language, which means that you put your commands into a text file and then they get carried out. In this case, they get typed into HTML files, which is what websites are made of. There are other scripting languages which get put into your HTML files, like JavaScript and VBScript, but they execute in the browser (i.e. on the computer of the person seeing the site), so one person may be running Internet Explorer on a Windows machine, another may be running Mozilla on a Mac, and another may be running Opera on a Linux machine (and many more possibilities and combinations, including WebTV, whatever that is).

Each combination (and I didn’t even mention different versions of the operating system and different versions of the browser) may execute one piece of JavaScript differently from another, and, unless you’re the person I mentioned first, running IE under Windows, VBScript won’t run at all.

I gave up using any JavaScript (except for the most basic functions) a long time ago, for that reason.

There are also languages which run on the server (they’re called server-side scripts), and they run the same no matter who is looking at the site, since they’re running on the server, not in the browser (the “client,” in network speak). I tried Perl a while ago, but it was too difficult for what I wanted to do, and I was ending up spending all my time scripting and none of my time writing.

Because I really only want to do one thing that HTML won’t do, which is to change what links a reader sees, and where they go, based on where the reader has been already. That’s it. The Carly chapter in U-town was designed to work that way. It doesn’t because I couldn’t make it work, and didn’t want to devote my life to learning how to make it work.

So, I gave up on doing this until I discovered PHP, and PHP does it perfectly. But the most important thing is that PHP does it without me having to devote my life to learning PHP.

PHP code sits right inside your HTML, so 90% of my files are straight HTML just like they always were. Just with a little extra.

For example, two thirds of the way through U-town, something very major happens. It’s not necessary to know this in order to enjoy the new novel, but if you’ve read U-town, it would be weird for it not to be mentioned in the new novel. But if you haven’t read U-town, any mention of this in the new novel would convince you that the characters had just gone insane. So, two chapters in U-town set a variable, then if that variable is set, you see extra dialogue in a couple of scenes in the new novel. If not, you won’t.

And, as I may have mentioned, doing this was easy.

Here’s the code which has to be in the U-town files:

< ?PHP
   session_start();
   $_SESSION[SEPARATE]=1;
?>

Here’s the code from the current file:

< ?PHP
session_start();
if ($_SESSION[SEPARATE] == 1) {  
     include('../inserts/03memory.php');
} 
?>

That’s it.

It is a little embarrassing how much I enjoy this.

Oh, and I should mention that PHP can do a lot more than I do with it, including that it was used to write WordPress, the application which runs this blog.

Next week, back to the characters, I promise.

(Note to myself: maybe Carly Stein should be the next character featured.)

image_pdfimage_print

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.