Getting Started With PHP.
Author: Uzi Levitovitch
Type: Web Programming
PHP
Level: All levels
Added: 26-02-2009
Rating:
Getting Started With PHP
Basic requirements for system:
A web server ( one of these )
– Apache 1.3 or 2,
- IIS (Internet Information Services. Comes with windows 2000 & XP)
- or similar.
PHP Coding Language
- PHP
MySQL Database ( required in future tutorials )
- MySQL
If you have a host that supports all of these programs then that’s great. If not you can install all of the above on a windows 2000 or windows XP system. I recommend using Apache over IIS as IIS has several security flaws which I have discovered to my cost previously.
PHP.net provides a useful installation guide on Apache 2 - follow those steps
http://www.php.net/manual/en/install.apache2.php
Once you have an installation on your local machine or a web server, let's proceed to..
Part 1
Your First Code
Open up notepad and write the following text into it.
<?php
echo 'This is a PHP script';
?>
Save the file as test.php making sure you change the ‘save as type’ bit in notepad to all files. If you don’t do this it can stick a .txt on the end of your file and it wont work.
If you are working on an Internet based server, use FTP to upload your file there.
Some things to point out about the script.
- In order to move from the HTML page to the PHP you have to open with a:
<?php
and close with a
?>
Every line of code within the PHP tags needs to be closed with a ; character. If this doesn't happen PHP will carry on reading the script until it finds a suitable or unsuitable closing character.
Save the file as test.php making sure you change the ‘save as type’ bit in notepad to all files. If you don’t do this it can stick a .txt on the end of your file and it wont work.
If you are working on an Internet based server, use FTP to upload your file there.
Some things to point out about the script.
- In order to move from the HTML page to the PHP you have to open with a:
<?php
and close with a
?>
Every line of code within the PHP tags needs to be closed with a ; character. If this doesn't happen PHP will carry on reading the script until it finds a suitable or unsuitable closing character.
In the next part we will consider basic variables.
Part 2
Basic Variables
Open up the file you created in Lesson 1 and delete all of the contents except for the opening and closing PHP bits.
File now looks like
<?php
?>
First we are going to set a variable. A variable is a name given to a piece of memory that we store information in while the script is running.
To create a variable we use the dollar sign, followed by the name of the variable:
<?php
$thevariable=’HELLO’;
?>
That script has just created a variable called ‘thevariable’ and it has stored ‘HELLO’ in it.
to output the contents of a variable we again use an echo statement.
<?php
$thevariable=’HELLO’;
echo $thevariable;
?>
This will output onto the HTML page ‘HELLO’; Notice we don’t need to put the variable in inverted commas when we are echoing. We only need to do that if it is a piece of text.
If we want to assign a number to a variable we do something similar:
<?php
$thevariable=1;
echo $thevariable;
?>
will output ‘1’;
If we want to add two numbers together we can do..
<?php
$variable1=1;
$variable2=2;
$result=$variable1+$variable2;
echo $result;
?>
This will output ‘3’;
Have a play and see what happens with those results. Use PHP as if it were a calculator to get some more interesting results.
Enjoy the result!



