Friday 11 May 2012

PHP - Where To Start - Part 0.1 - Genesis

In The Beginning there was only darkness...Then the power came on and someone thought it would be cool to have a scripting language that ran on the server side but was still processed from within the web server it's self, without needing to run off and buy some application resources from a service down the street that it didn't really get on with in the first place.

PHP is that scripting language.  It stands for (rather puzzlingly) Hypertext PreProcessor, however it was originally coined as (somewhat more understandably) Personal Home Page. The guy that had the thought that it would be cool also had the thought that it would be way better if it was free for people like you and me to play around with (just like your mum's cleaning products were when you were playing at making explosives). His name was Rasmus Lerdorf and he single handedly wrote the first two versions of PHP.

And so we have PHP, accessible to all.  Whole communities have even been built around it's humble foundations.  It's impact on the web is immeasurable. And now were going to cover the basics.

This is an analogous description of the fundamental information about PHP.  It's not meant to be taken as a gospel truth, but rather its meant to help get some key concepts to stick so you can get on with the fun stuff.  

PHP is, as I said in that nonsense at the start, a scripting language.  This means that it does not require a compiler to run, it just shoots down each line of the code as if running the hurdles.  It's very lite, and apart from the PHP software it's self, and a web server software, you don't need any special toys to make it come out to play.

If you are running windows, the best way to get from nothing to a working php setup is (in my all devouring opinion) to go download XAMPP from www.apachefriends.org and install it.  I suggest installing it to c:\xampp, but where you put it is your call, just know that you are going to want to get into that folder a lot. One important thing I will say is not to use ANY spacial characters in the path name, so no ~'s or #'s or anything like that. This isn't an issue for the PHP side of things, it is however a huge problem if you every want to use the PEAR application.

Once you have a working environment you will need something to make your php pages with.  This bit's as easy or as hard as you want to make it.  You can simply use notepad (NOT wordpad - notepad, they are different) but this will give you no visual aid when coding.  You can download for free a coding specific text editor like pspad, notepad++ or any number of other alternatives.  You can download, again for free, the eclipse IDE for java and then download the PHP Language add on.  Or you can spend some money and buy an IDE such as Dreamweaver or Zend Studio Pro (I actually do not recommend the use of an IDE for someone who is just beginning, they have a lot more in them - especially Zend Studio - than you will use at this level, and the extras make getting confused a lot easier).

After you have settled on your application you're almost ready to get on and make your first php page.  Open up your program and choose new file (as I have no idea what program your likely to be using your going to have to work out this complication on your own).  You want to then go immediately to Save As... and navigate to where your web root is.  If your using XAMPP that will be in the directory that you installed XAMPP to and then into the directory called "htdocs".  Once there save the page as "index.php" making sure that, if the program supports it, you have selected the extension type as php and if the program doesn't support php file extensions you select "all files" as the file type.

Now you have, in effect, created your first php page.  How'd that feel? crap? really? Yeah I get where your coming from, but let's be fair, if I told you to put the 3 lines of code in the file that would have had show "hello world" you wouldn't have felt any better about it, would you? especially as I haven't gone over how to actually view the fruits of your coding loins.

First we need to make sure that the web server is running, so in XAMPP (if your not using XAMPP I'm gonna assume you went that road because you know what your doing, and I'll will leave you do your thang) You want to launch the XAMPP Control Panel, either from the start menu, or from the desktop shortcut (sometimes on Windows 7 you will get a message about this thing must be run from someplace that your sure is where it was just run from, you can normally just ignore that and click whatever button it presents you with).  In the control panel have a look at the list under Modules, the top option should be Apache.  If this has a green "Running" beside it, your all good, if not click the "Start" button and watch the white box.  you should have the Running message displayed in a few moments.  OK, so now were running a web server.  This is important, because your PHP pages need to viewed through the web server to be processed. So if you want to view your PHP page, it's pointless going to C:\xampp\htdocs\index.php because that's just going to show you the raw code. What you need to do (This Is VERY Important!) is open up your Internet browser and then find the address bar.  In there change the content to be http://localhost/index.php and there you can see your page.  It's blank just now, but as we move through sections we'll get to that.

One final, impossible to progress without knowing, thing before we drop into the coding of our very own super-fantastic PHP pages, EVERY PHP Page MUST have
<?php
at the start and
?> 
at the end. This is what tells the web server "process my damn code fool" and without them, everything will go horribly horribly wrong.

Right, on to Part 1 then...

No comments:

Post a Comment