Friday 11 May 2012

PHP - Where To Start - Part2 - Getting It On The Screen

OK, so in Part1 we got to grips with basic operators.  But we have nothing to show for blood and sweat.  Let's remidy that.

There are a few ways to get PHP to display to the screen, and while they may apear on the surface to all do pretty much the same thing, they are all different and all there for a reason. In no perticular order here are two of the most common methods:

echo
print

As well as those, there are other commands for more precise displaying of information, but they arn't related to this level of coding. It's as well noting just now that there is a big difference in the way that the PHP processor handles single quotes ( ' ) and double quotes ( " ).  I will ilustrate this difference in the code section for this page.  If you recall, in Part1 there was a line at the end of the code section that started with the following on it:
//EOF...
On the line just above that add the following code using copy and paste, and then save and view the page in your browser, as coverd in Part 0.1

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...

PHP - Where to start? - Part1 Operators

So, your new to PHP and programming in general.  You're also not a season ticket holder for your local book reading club.  But you do want to learn some PHP for whatever reason.  And that's where the inevitable question comes from : "Where do I start?"

Until I made this post and can now, in complete arrogance, turn round and provide the answer of "Here", this was not as easy a question to answer as "where not to start".  Lets face it no one really looks up a "how to code" book or site to have the first portion of the book/site send you to sleep with the history of the language and the principle ideals of OOP.  As important as these things are that's not what your here to learn, so lets move swiftly on.

Each time I'm asked "where should I start" I always reply with "operators".  It's kind of like trying to learn mathematics, you need to know what +, -, /, *, = and all the rest, all mean before even the simplest tasks make sense. The way I like to look at the main operators for beginning PHP is to split them into three category's : alteration, comparison and assignment - these are other 'official' classifications, and .  Alteration operators change the value of something depending on what the original value was, these are the familiar
+       -       *       /
operators, but there are also ++ and -- operators.  these increment (increase by 1) and decrement (decrease by 1).

Comparison operators are used when we need to check the value of something against certain criteria. these operators are :
<   ~~Less Than
 >  ~~Greater Than
>=  ~~Greater Than OR Equal To
<=  ~~Less Than OR Equal To
== (yes it's meant to be 2 of them)  ~~Equal To
and === (again, that's meant to be 3 of them).  ~~Identical To
All of the operators above can be prefixed with ! to make it NOT, so !< would be NOT Less Than, or  whichever operator you are using.  I say "all" but actually, when using ! with == or === you don't just add it to the left of them, you actually replace the first = with the !, so:
!=    ~~NOT Equal To  - and -
!==  ~~NOT Identical To
The times that you make use of <= and >= you need to be careful and put them in that order, changing the order changes the operator.  you also need to be careful (and this one is tricky) to get out of the habit of using a single = to check if one value equals another.

Assignment Operators are a bit shorter.  Here at the beginning we are only going to look at one of them :
=
this assigns the value that is on the right of the operator to whatever is on the left of it.

OK, so lets get some code up already so we can see these operators in their natural habitat.



Right, so you seen that rouge operator in there? that's one of PHP's handiest shorthand operators, it's a concatenation operator.  This means that instead of just replacing the contents of the variable on the left of it, it appends the value on the right onto the end of it.  This only works if the variable has been declared (the line where it is assigned as an empty variable just above the while loop).

So there we have covered the basics of operators, and seen how they work in code.  But if, like me, you have trust issues, and you want to see what's in each of those variables with your own eyes, you'll be wanting to move onto Part 2 - Printing to Screen (just as soon as I post it...).