Before I start I really want to emphasize that this is a tutorial for the absolute beginner! If you have experience with php I recommend you stop reading, if not this is the tutorial for you! If you have some web space available to use the code as I give it to you please do so, otherwise try downloading WAMP server to test it on instead.
First It's best to check your servers php information:
<?php
phpinfo();
?>
Save that to a file called "phpinfo.php" and upload it or install or put it on your localhost folder, which ever is relevant to you. This will bring up a large page containing all the information on your php installation, from it's version to all the Libras (extra optional function sets) installed on it. This also allows us to find out if php is definitely installed on the server. Assuming php is installed correctly on the server please read on.
Blocks of php are usually stared with a "<?php" and finished with a "?>". There are other ways of doing it, but this is the usual way to do it. So some php code would look like this:
<?php
echo "Hello World!";
?>
This code will simply display the words "Hello World", with the echo function telling php to display whatever is within the quotation marks.
The next step up from displaying text, will be using commenting in php. Php comments, are similar to HTML comments in many ways, except it is impossible for visitors to see them, even if they choose to view the source There are several ways to comment in php, single lint comments like this:
<?php
//Single Line Comment!
?>
Or block comments like this:
<?php
/*
Amazingly, Shockingly and
Incredibly cool comment
block. WOW!
*/
?>
These are useful for reminding yourself what different sections of the script do.
The next step up is the use of variable! We're starting to get somewhere now! Here's an example:
<?php
$message = "Hello World! I'm a variable!";
echo $message;
?>
This bit of script create's the variable called message and stores the text within the quotation marks in it. As you can see the variable has to start with a "$" sign. In php you don't have to declare variables, like you do in asp. There are a few rules with variables, firstly the must be named with just alpha-numeric characters or underscores (a-zA-Z0-9 and _). And they must start with a letter or an underscore. Variables can be used to store numbers too like so:
<?php
$one = 1;
$two = 2;
$answer = $one + $two;
echo $answer;
?>
This code will take the variable $one (with the value of 1) and the variable $two (with the value of 2). And then adds the two variables together and stores them in the variable $answer and then displays them using the echo function.
We shall now look at getting variables from the URL query string. You know how some websites have URL's like:
http://www.example.com/index.php?page=news
This is creating a variable called page and giving it the value news. To refer to this variable in a php script we use do this:
<?php
$page = $_GET['page'];
echo $page;
?>
The Super Global $_GET refers to GET variables (variables passed to the server via the URL) and then gets the one called page and gives it whatever the value is that is set in the URL and then stores it in the variable named $page. Finally it uses the echo function to display it.
This is all for now folks, but I shall be uploading part 2 soon! To see when that's added subscribe to the rss feed! Hope this has been useful!
EDIT: Part 2 Now Available: Here