-------A Small Note-----------------------------------------------
PHP is server side scripting, so it will not work on your computer unless you have something like
WAMP installed or you have a php enabled server you can upload to.
-------About PHP-------------------------------------------------
OK so you've decided to learn php, first thing you may want to know is what php is capable of doing for you it has many uses, it is used all across this site in fact its mainly the only programming language I have used on WeLink but the main use people would want to learn php would be to integrate your site with a user system of sorts.
-------PHP, Very Basics-------------------------------------------
OK here we will get down to the coding parts, most probably if your reading this would be what you want to learn about.
To start you off here is a extremely simple program, and here it is:
<?php //opening php tag
echo "This is my page"; //php code telling the web server to treat this is my page as html
?> <!--closing php tag-->
Now for the explanation of it all.
you see the
<?php and
?> these are called PHP tags and tell the web server where the php code starts and finishes. So anything outside this will be considered normal HTML.
On line 2 the first piece of code is
echo, this tells the web server to treat the following piece of text contained in the quotation marks
" as regular html, so
This is my page is what is printed.
The quotation marks must be used if the string you want to print contains characters, if it just a numeric value you can get away without using them.At the end of this line you may of notice the semi colon
; this is very important to be included at the end of every line.
Now When you upload this to your browser you should see
This is my page
I know this isn't very exciting but stick with it, it gets more interesting I promise.
---------Variables----------------------------------------------------
Variables are a very useful part of php.
PHP variables are represented by a dollar sign followed by the variables name, there are special variables called superglobals which have special functions but I'll go into that later.
The variables name itself must follow a few particular rules for it to be valid, these are that the first character of the variable must be an underscore ("_") or a letter (a valid letter to use are a-z, A-Z and the ASCII characters from 127 through 255).
<?php
$variable = 'Hello';
$Variable = 'Bill';
echo "$variable $Variable"; // outputs "Hello Bill"
$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.
?>
Variables are very important as this is how you transfer data from one part of your script to another.
There are a few pre-defined variables in php such as $_POST, $_GET, $_SESSION, $_COOKIE etc.
For the complete documentation on these you should
read thisI will show you an example of how the variable $_POST is used, along with other standard variables.
First create 2 pages, name then page1.html and page2.php
Here is the code for your first page. (page1.html)
Your Name: <!--text explaining the input box-->
<form name="name" method="post" action="page2.php"> <!--details of how the form will behave-->
<input type="text" name="yourname"> <!--details on the input box-->
<input type="submit" name="Submit" value="Submit"> <!--the button which submits the form-->
</form> <!--the form ending tags-->
This is a simple form code you would know about this is you've used html.
The parts that are relevant to this program are on the first line
action="page2.php" this tells the browser where to forward you to once you've filled out the forum. On the second line
name="yourname" this is important when you get to making your own that you put relevant names here as your going to have to remember it later on.
Now onto where the magic happens,
The second page will have contain the code:
<?php
$yourname = $_POST['yourname']; //retrieves data from the input form
echo "Hello ".$yourname; //prints the data
?>
Name the page2.php it is important to give every page you create containing php the suffix of .php or the web server will ignore all your <?php and ?> tags
You will notice this is not that different from the first piece of php code
$yourname this is a variable you can tell from the leading
$ you must also give these relevant and rememberable names for ease later on during your coding.
When you open page1.html and enter your name, for example if your name is John after submitting the form the new page will load showing Hello John.
The
$_POST variable is used to 'catch' information from forms that have the input type of submit or post.
The
['yourname'] following it tells it which piece of data to catch from the form, you should remember from before what you named your input box as notice that was given the same name as this. This is because its that curtain piece of data your php is waiting to receive. If you have a more complicated form you would have many more of these being used but for now we will only use the one.
The entire line of
$yourname = $_POST['yourname']; simply tells the server to set the variable
$yourname to the value of whatever yourname was entered as in the form.
In the line bellow the echo command is used again you may also of noticed the full stop
. and that
$yourname isn't surrounded by ", the full stop is there to tell the server that there's another piece of information stringed to this line afterwards. This is used instead of writing everything out long hand like
echo 'Hello'; echo $yourname; as this would become very tedious especially when you come to typing out large pieces of code. Finally
$yourname isn't surrounded by " simply because you don't need to use them around variables.
And another note on the use of the full stop you can use it to add to the end of an existing variable by
$a .= $b;REMEMBER:
* $ followed by text with no spaces means its a variable
* Everything you want shown with a echo commend must be surrounded by ' or " unless its a variable
* To join pieces of code place a . after each piece and put a ; after every line of code