Do you want to learn PHP?
I’ve been coding (or scripting if you want) PHP actively for myself for probably 10 years now, over the years I’ve had a lot of questions about it and have done a lot of small favours for people. I would be happy to give away some of my knowledge, so please ask if there is anything.
I am going to teach PHP from basic to advanced, I will try to keep it to one article every other day.
Subscribe to my feed if you don’t want to miss out on this chance.
Basic Introduction
What is PHP? It is a scripting language that resides on the server-side as opposed to javascript which is downloaded and ran by the client (which is called client-side).
To be able to execute PHP scripts, you need access to a webserver that runs PHP scripts – there are a lot of alternatives.
I am not going to explain them here, but I am sure it is possible to arrange something if you need a place and is into learning.
It is also possible to run an Apache server locally with PHP.
My First PHP Script
Create a file named first.php and this is the contents
<?php
echo “hello (word)”;
?>
Save this file somewhere on the webserver and try to go to the url.
It will display: hello (world)
Explanation
The first line is <?php, this tells the webserver that it is about to parse PHP scripting. You need this before every piece PHP script for it to be executed as PHP.
The second line is echo “hello (world)”;, this is actually the PHP script line.
It uses the built-in function ‘echo’ to display a string, the string needs to be contained within quotes.
And notice that the ending of the line is ;, this finishes off the statement.
If you were to forget the ; and had several lines of code the server will continue to feed the rest of the code into the echo function, which would probably not work or give really really unexpected results.
The third line is ?>, this tells the webserver that the PHP script is over.
When it reaches this tag, it will continue to output the rest of the page to the browsing user. This means that you can use PHP in between HTML to generate dynamic responses.
My Second PHP Script
Heck, since it’s the first post and the first script was so easy.. I will show you another script and teach how variables work.
I will move on to more advanced things later, so keep watching if this is way too basic for you. There will be more blood!
<?php
$name = “Espen”;
$age = 23;
$twice = $age * 2;
echo “My name is ” . $name . ” and my age is ” . $age . “, if I had lived twice as long I’d be ” . $twice;
?>
Explanation
The first line of PHP script is $name = ‘Espen’;.
The dollar sign ($) indicates that this is a variable, variables are used to contain dynamic information like if you want to display it at several places in your document.
The variable name is actually ‘name’, and it is set to contain anything behind the equal (=).
The string behind the equal sign is “Espen”, notice the ; ending again.
Because it is contained within quotes, PHP automatically identifies it as a string, it will not work without quotes.
I will show some examples between the difference in double and single quotes later.
The second line is $age = 23;.
As before, notice the dollar sign and also notice the ; ending.
Because age is an integer, you don’t need quotes. If you use quotes around the age like ‘23′, PHP will identify it as a string and doing calculations on it can fail.
The third line is $twice = $age * 2;
This sets the variable named $twice to contain whatever is in the variable $age and multiply it with two.
We know from before that $age is set to 23, so this will yield a result of 46. $twice now contains 46.
The fourth line is:
echo “My name is ” . $name . ” and my age is ” . $age . “, if I had lived twice as long I’d be ” . $twice;
One of the methods of combining variables into another is the usage of the period character (.) There are other and better ways to do it, but that may be to complicated for a beginner for now.
Some if you may relate this to javascript where they use + to combine variables.
So this actually feeds a lot of parts of information to the echo function to display it:
“My name is ” is the first part, the next variable will be printed right after it – so notice the white space before the last qutoe.
Then the period character is used to append the variable $name to it.
The string ” and my age is ” is appended by . again, notice white spaces before and after the string.
You probably understand the rest of it now.
Notice that any white space added that is not contained within quotes will not be displayed either.
If anything is unclear or completely wrong, feel free to drop me a comment!
More learning is coming: Subscribe to my feed.
Tags: hello world, learn php, php, scripting, teach php