Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tim's PHP 101 tutorial
#1
I wrote this a little while ago for another site, they are no-longer around so I thought this site was a worthy site to put it to use on. This is 100% written by me and I give no permissions for it to be reproduced in any way without my prior consultation. It should not appear anywhere except on here and my personal blog (if you find it somewhere else, let me know).

If I have made a mistake or missed something please let me know.

===Lets begin===

Here is your introduction to basic PHP.
Today you will learn about:

* Development environments
* Outputting
* Variables
* Selection
* Functions
* Inputting data


Development environments:
Before you start you will need a way of testing your PHP scripts. This can just be some free web hosting with PHP enabled but it is easier to setup a testing environment on your own computer. You may either install Apache and PHP separately or get one package which includes both Apache and PHP as well as a few other applications in one installer. I would recommend the second option. Some examples of these are XAMPP and WAMP. These both come with apache, PHP and MySql as well as a few other things. I personally use XAMPP. Once you have installed your chosen software go into the directory in which were installed in and you will see a folder called HTDOCS. This is where you place all your scripts (I recommend placing a shortcut to this on your desktop). Now open up your browser and type into the address bar localhost. This is how you access the scripts that are in your HTDOCS folder. Eg. If you have a file called test.php in a folder called example you would type in localhost/example/test.php.

Now you are ready to start coding. You can do this in notepad or any other text editor but I would recommend something with line numbers such as notepad ++.

Your First Script/Outputting data:

Everything you code in php must be placed inside these delimiters <?php ?>.

Start by creating a new document in your editor of choice and type this:


PHP Code:
<?php
echo “hello world!;
?>

Then save it in the HTDOCS folder as hello.php. Now you can go to localhost/hello.php and you will see hello world.

echo is used to output something. If you wish to output a variable (which we will get to in a minute) you must use “” whereas for plain text/html you may just use ‘’. “” Will work in both instances so normally people will just use that. All lines of code must be exited by a semi colon ;

Here are some more examples of outputting things:

PHP Code:
<?php
echo ‘Test’;
echo 
“Test”;
echo 
$test;
echo 
“$test”;
echo 
<b>test</b>:
echo 
<b>test</b>;
echo 
<b>Name$test</b>;
?>

Variables:
You may store and process data in PHP by using variables. All variables are identified by a $ sign at the beginning of them.

Eg:

PHP Code:
<?php
$Name 
‘John’;
echo 
“My Name is$Name”;
?>

You can treat variables just like regular data.
Eg:
PHP Code:
<?php
$num1 
5;
$num2 10;
$num3 $num1*$num2;
Echo 
$num3;
?>

This will output 50. (To add things use +, to subtract use -, to multiply use * and to divide use /) You will notice that integers do not need to be encased in “” or in brackets.

Here are some more examples:

PHP Code:
<?php
$word 
<b>Word!</b>;
$word2 “The word is $word”;
$num1 5;
$num2 15;
$num3=12;
$num4 ($num1+$num2)*$num3”;
?>

Selection:
In PHP you will often need to select something which meets a certain condition. PHP has maybe ways of doing this but the most versatile and common way is the if statement.
Eg:

PHP Code:
<?php
$question 
false;
if(
$question == false){
echo 
‘False’;
}else{
echo 
‘True’;
}
?>

Some notes on this:
== means same as
=== means identical to
!== not identical to
!= means not the same as
< means less than
> means greater than
&& means and
Or means or.

There are many abbreviations of these and many other operators but these are the main ones you will use.

Basically what happens in that program is that it creates the $question variable and sets it to false (Boolean values don’t need to be encased in “”) then the if statement check is the variable is false and if it is it outputs false and if it is anything else it outputs true.

If the requirement is met it will perform everything between the {} tags. Then if it is anything other than that it performs the actions between the else {} tags.

Examples:

PHP Code:
<?php
$condition 
5;
$condition2 10;

if(
$condition == 5){
echo 
‘the number is five’;
}else if(
$condition2 == 10){
echo 
‘the second number is 10’;
}else if(
$condition1 == $condition2){
echo 
‘the numbers are the same’;
}else if((
$condition1 == 5) && (condition 2 == 10)){
echo 
‘the numbers are 5 and ten’;
}else{
echo 
"the numbers are $condition1 and $condition2";
}
?>

Functions:
If you are doing the same thing over and over again it may save some time to use a function.

Eg:

PHP Code:
<?php
function check5($num){
if(
$num == 5){
echo 
‘the number is 5’;
}
}

$innum 5;
check5($innum);
?>

The above program will output “The number is 5”. This is because it replaces $num with whatever the new variable is and does the steps you have specified above.

Examples:

PHP Code:
<?php
function hello(){
echo 
‘hello’;
}

function 
count($num1,$num2){
$count “$num1 $num2”;
echo 
$count;

hello();
count(4,10);
?>

Inputting data:
Finally, you will most likely need to input some user data. This is done in two steps, the first is inputting data with a html form and the second is where PHP processes the data. When a form is submitted it goes to another page as specified as the action of the form, it then passes the data to that page by either get or post, get is through the address bar and post is “invisible”.

Here is an example:


PHP Code:
<body>
<
form name="input" action="thispage.php"
method="get">
Name:
<
input type="text" name="name">
<
input type="submit" value="Submit">
</
form>

<?
php
$name 
$_GET[‘name’];
echo 
$name;
?>
</body> 


This script if named thispage.php would display the name inputted by the form. The $_GET part gets the data but if the form method was set to post rather than get you would need to use $_POST.

Examples:


PHP Code:
<body>
<
form name="input" action="thatpage.php"
method="post">
Username:
<
input type="text" name="username">
Password:
<
input type="password" name="pass">
<
input type="submit" value="Submit">
</
form>

<?
php
$username 
$_POST[‘username’];
echo 
“Your username is $username”;
$pass $_POST[‘pass’];
echo 
“Your password is $pass”;
?>
</body> 

Including files:
Rather than typing large amounts of code over and over again it is possibly to include one php script in another.

Eg.

In phpfile1.php

PHP Code:
echo 'Hello World'

In phpfile2.php

PHP Code:
include('phpfile1.php'); 

When you run phpfile2.php it will display hello world.

And that should be enough to get you started with PHP. As I wrote this in MS word it may have changed a bit of the syntax such as made lowercase letters uppercase. In general most things are lower case with the exception of GET and POST. I also may have made some small syntax errors, please let me know and I will fix this up.

And that's it Smile Your quick intro to PHP, that should get your started with making a few small dynamic sites. I will do mysql and text file interaction in another tutorial.

Links:
XAMPP: http://www.apachefriends.org/en/xampp.html
Notepad++: http://notepad-plus.sourceforge.net/uk/site.htm
Reply
#2
This seems very useful, thank you for posting.

I will have a thorough read when I have more time, it seems like an area I would like to start learning.
[Image: 3326yvl.jpg]

Reply
#3
No worries, again, if anyone finds any mistakes let me know, I think one or two was found back when I wrote it and I can't remember if this was the updated version or not.
Reply
#4
I'm going to give this a try tonight. Being new to php, I'm taking full advantage of tutorials like this!
Reply
#5
Great! If you need support with anything from the tutorial feel free to post in this thread.
Reply
#6
cute tuto......
cute tutorial.. for the begineer's....
Reply
#7
Thanks for this.
Reply
#8
Awesome!
Trying to teach myself PHP, so far I've learnt how to create dot points, and paragraphs. Hopefully this guide can sink in a bit more then others.
Reply
#9
(10-08-2009, 08:25 AM)hinstera Wrote: cute tuto......
cute tutorial.. for the begineer's....

(10-08-2009, 09:18 PM)Extasey Wrote: Awesome!
Trying to teach myself PHP, so far I've learnt how to create dot points, and paragraphs. Hopefully this guide can sink in a bit more then others.

No worries :p
Reply
#10
nice work

thanx
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 534 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 2,789 10-10-2011, 01:00 PM
Last Post: Greyersting
  [Tutorial] How to grab IP adress with PHP flAmingw0rm 29 6,722 11-26-2010, 02:24 PM
Last Post: Win
  PHP mysql Login Tutorial By MAcar V1.0 MAcar 8 2,521 10-08-2009, 04:57 PM
Last Post: MAcar
  PHP site backup tutorial andrewjs18 6 1,396 10-07-2009, 11:27 PM
Last Post: andrewjs18

Forum Jump:


Users browsing this thread: 1 Guest(s)