Saturday 13 April 2013

PHP: from zero to... Hello World

Right, so I'm commencing this process of getting my brain around PHP. First up, I need to get the thing installed on this machine. I had it installed and gathering dust before,  but I completely rebuilt this thing recently when I migrated to Windows 7 (from Vista. Go on: laugh), so I need to start again.

I am typing this as I investigate.

My first hurdle is I get to the PHP download page and discover a few things:
  • I'm so naive I don't even know what the current version of PHP is. It seems like it's 5.5
  • But they don't have the Windows install for 5.5, they're only up to 5.4. OK. I don't really care.
  • However they're asking me whether I want a Non-Thread-Safe version or a Thread-Safe one.
Well I don't bloody know! On the face of it I want the thread-safe one, cos... well... given the option being thread-safe has to be better than being not thread-safe. Surely. But obviously there's more to this than meets they eye, as I'm sure they'd not be offering both options if only one of the options was the sensible one.

I have googled "php download threadsafe non-threadsafe", and landed on a Stackoverflow page which seems to answer the question: if installing as an Apache module (given some other criteria), then use the thread-safe one. I'm gonna be installing as an Apache module, so this is the route I will take.

There was also this really handy article too: "Difference between PHP thread safe and non thread safe binaries".

It's interesting to be having questions like this even before downloading, compared to ColdFusion's download & double-click on setup.exe (I know the filename is not setup.exe, that's beside the point). But it's good that everything's already on Google for me. And, that said, this is only a dev machine so I think threaded-ness is not really going to be an issue for me one way or the other.  Off I go with the download.

Blimey. PHP is only 15MB. CF10 is 350MB. Interesting.

Then thing that I downloaded was just the files, zipped, so I'm having to configure it all myself. No probs... I was expecting this. I also know there are .msi files around the place, but the docs recommend doing it manually, and copying files and changing text files shouldn't be a challenge, so I'll do it manually. There's an install.txt file which explains what to do, but here's what I'm gleaning from that:
  • Unzip the files and stick 'em in C:\apps\php\5_4
  • Add that dir to my Windows path
  • Next I had to configure a php.ini file. The docs suggest using the production config option, but as this is a dev machine that didn't sound like good advice (the sample files they have are for production or dev). I diffed the files and the only differences are some error- and logisitics-reporting stuff being switched on in the developer config, so I'm running with that. It's the equivalent of having robust exception handling switched on or off in CF as far as I can tell.
  • I cranked up Apache and added a new virtual host for my PHP stuff (and put a hosts entry in etc... I'll presume you either know how to do that, can find out for yourself, or don't care: I'm not explaining it, anyhow), testing Apache was still working having been lying around unused for quite a while. It serves up HTML files from the new domain (php.local, should it matter) and virtual host.
  • Now I gotta work out how to get Apache talking to PHP...
  • ... which proves to be very easy if the docs are there in front of me:
    • I tell php.ini where the doc root is:
      doc_root = C:/webroots/php
    • and load a module in Apache, telling it where to find PHP:
      LoadModule php5_module "C:/apps/php/5_4/php5apache2_2.dll"
      AddType application/x-httpd-php .php
      PHPIniDir "C:/apps/php/5_4"
  • To test that I now restart Apache (just to make sure it does still start, which it does).
  • In C:\webroots\php I stick a file helloWorld.php, and employing my finest PHP skills, knock this together:

<?php $msg = 'Hello World'; ?>
<title><?php echo $msg; ?></title>
<h1><?php echo $msg; ?></h1>

Which indeed works. This impresses me, because I actually remembered that from years ago when I last installed PHP. It's probably not the best way to do solve that particular coding challenge, but I'm just testing that the thing's actually running!

So there we go... Hello World. It's not exactly earth-shattering, but pleasing enough for me for the time being. I'm afraid to say this will be the level at which I will be pitching my PHP articles to start with. There is going to be nothing exciting for people who know anything about PHP, as this will simply be a log of my efforts to learn it.

Oh, btw: if you're reading this and do know about PHP and find yourself going "No, Cameron! Nononono, not like that!" then please tell me.

I'm gonna find out now whether Zend Studio is still where it's at with PHP IDEs, and get that re-installed if so. Then find some PHP tutorials to mess around with.