How do I?
09 February, 2010, 08:21:22 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Please stumble us Smiley
 
   Home   Help Search Login Register  
WeLink Del.icio.us Digg FURL FaceBook Stumble Upon Reddit Google Bookmarks Squidoo Technorati Yahoo My Web

Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: PHP tutorial, the very basics  (Read 1828 times)
renlok
Administrator
Sr. Member
*****
Offline Offline

Posts: 381
hPoints: 1151


View Profile
« on: 22 July, 2008, 03:28:39 AM »

-------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:

Code:
<?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
Quote
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).

Code:
<?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 this

I 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)

Code:
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:

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
« Last Edit: 26 September, 2008, 08:47:32 AM by renlok » Logged
renlok
Administrator
Sr. Member
*****
Offline Offline

Posts: 381
hPoints: 1151


View Profile
« Reply #1 on: 26 September, 2008, 08:45:18 AM »

---------Arrays------------------------------------------------------
PHP.net documentation
What is an array?
When working with PHP, sooner or later, you might want to create many similar variables.

Instead of having many similar variables, you can store the data as elements in an array.

Each element in the array has its own ID so that it can be easily accessed.

There are three different kind of arrays:

  • Numeric array - An array with a numeric ID key
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

Numeric array
example 1:
Code:
<?php
$array 
= array('john''pete''dave');
?>
example 2:
Code:
<?php
$array
[0] = 'john';
$array[1] = 'pete';
$array[2] = dave;
?>
A Numeric array can be set either way, if you set the ID key manually like in example 2 each element is accessable with the key you set it to i.e. for john the key is 0. If the ID keys are automatically assigned like in example 1 the keys are set in order starting with 0.

They are accessed simulally to variables
Code:
<?php
echo $array[0] .', '$array[1] .' and '$array[2] .' are friends.';
?>
this will print john, pete and dave are friends.

Useful Sites:
http://php.net

To Be Continued

If there are any errors with this tutorial please tell me and I will fix them also if there's anything you think needs adding or changing.
« Last Edit: 26 September, 2008, 08:48:40 AM by renlok » Logged
Pages: [1]   Go Up
  Send this topic  |  Print  

 
Jump to:  

Powered by MySQL Powered by PHP Archive Version
Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
Valid XHTML 1.0! Valid CSS!