Friday 11 May 2012

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

No comments:

Post a Comment