So I’ve always wanted to learn php. It’s a type of server side scripting language which is used to create dynamic websites. It was created by a chap called Rasmus Lerdorf, who even though his name sounds like a Bond villain, actually changed the world in ways you can’t imagine – and for the better. He created the php scripting language and when her first built it, he called it “personal home page” but it soon changed to something sounding a lot more complicated which was “Hypertext Preprocessor”. In 1995 it was very foresighted and still is.
The official home of php is php.net. There you’ll find everything you need to know about how it works, what symbols mean, how to use those symbols and what you can do with php. What can you do with php? Anything – is the answer. One thing you have to remember with php is, it’s a language – practice, practice, practice! Otherwise, you’ll learn something and forget it the next day.
I’ve started a php web programming short course for beginners at City University London on a Tuesday night. At the time of writing this I am in my second week. Already, I think my head is about to explode, but I’ve realised what a fundamental thing php knowledge is in this day an age. It’s as fundamental as knowing how to create fire if you’re working in the tech or media industry.
And a big round of applause for my genius employers for funding this fundamental character development!
What you need for writing php
Basic stuff to remember with php programming
When you’re writing a php script it starts with “<?php“.
When you write something, to end the phrase in that script, end it with “;”
Whole scripts end with “?>”
Script means code, this is different from a string, which I’ll get too.
“Echo” means get or run whatever it is you’ve created.
A variable is like a box, you name the box and you give it a value, so that when you open/echo/run the box, it’ll show what it is you want, variables also start with “$” – so for example $animal = ‘kitten’; (Like most things, they won’t do anything unless you pay them I guess)
‘animal’ is the name, ‘kitten’ is the value.
You can have different data types (what’s in the box/variable)
1. Integers – which are simple whole numbers
2. Floats – which numbers with decimal points
3. Strings – words, which are displayed in single quote marks
4. Boolean – data types which carry a TRUE or FALSE value, TRUE or FALSE will determine whether something works or not (Excuse the simpleton explanations, this is just how I understand it so far…)
So, if you’re writing code and you want to show a variable it would look like:
$animal1 = ‘dog’;
$animal2 = ‘cat’;
echo $animal1 . $animal2;
When you upload this script to a server or a localhost then you will see “dogcat” on a blank screen. And that is the result of using a couple of variables. You’ll notice in the code above that there is a dot, think of the dot as a piece of glue, adding the two together, but not mathematically adding it, instead literally putting one next to the other.
If you’re writing a piece a code and you want to make a note of something then you can add a comment. Comments start and end with // or ##.
Operators are pieces of code which make your data/variables do something. So adding, taking away, displaying what is or isn’t equal.
The basic ones are:
Plus + Divide / Multiply * Minus – Modulo %
The above all look pretty self explanatory… Modulo, is the sore thumb. Modulo is the remainder of a divide equation. For example – 3 divided by 7 is 2, and you have 1 as the remainder. 1 is the modulo. If something divides perfectly, then 0 is the modulo. 0 is FALSE in php code… We’ll get to this – but remember it for now.
Increment and decrement
This is adding and taking away, increase and decrease. ++ means add 1, — means take away 1.
Where you put them in the code depends on the answer. If you want the increase or decrease to happen then put them first.
$x = 10
$y = ++$x
$y = 11
If you don’t want to work then put them after…
$x = 10
$y = $x++
$y = 10
$y is still 10. That’s the just the way it goes folks.
Equations which are in brackets are carried out first. Remember that too! Remember BODMAS at school? The order of operation? Well, Google it, and know that the same applies to php when running your scripts.
There are logical operators but we’ll get to those very soon! So… is your brain hurting yet?