Be a Supporter!

PHP: Beginners tutorial

  • 5,060 Views
  • 42 Replies
New Topic Respond to this Topic
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
PHP: Beginners tutorial 2005-11-03 23:40:42 Reply

Beginners PHP Tutorial

In this tutorial you will learn:

* What PHP can be used for
* How PHP works
* A basic PHP script
* Using variables
* Using some basic conditional statements

Chapter 1 :: What PHP can be used for

PHP can be used to create interactive web applications. PHP can allow you to easily create simple web applications, or with a little more knowledge you can create massive, feature rich we applications.

Here are just a few examples of types of software and applications that can be created with PHP and MySQL (A popular database solution).
[list]
* Forum application (The one you are using right now was created using PHP and MySQL.)
* Guestbook
* Content management system
* Mailing list
* Chatroom
* Support ticket system
* Link management system

These are just a very small number of things you can create using PHP and MySQL.

Chapter 2 :: How PHP works

PHP is embeded within HTML. PHP files end with the extension ".php". To better show how PHP works with HTML and the web server, here is a small example script:

<html>
<body>
<?php
echo 'Hello world!';
?>
</body>
</html>

Lets say we saved that page as test.php. Here is the chain of events that would happen when that page is viewed:

* The page is requested by a user.
* Before the web server sends the page to the users browser, it sees the .php extension.
* Because the page has a .php extension, the server interprets all of the PHP code within the page.
* In this case, the server looks at where we echoed "Hello World", and it places those words between the <body> and </body>.
* The parsed page is then sent to the users browser, where the only see the words "Hello World".

If you did this example, and then looked at the page and viewed the page source, you would notice that there is no PHP code. This is because all of the PHP code on a page is parsed on the server, and never reaches the users browser. Only the output from the code is sent to the browser.

Chapter 3 :: A basic PHP script

In this chapter, we are going to go over the elements of a very basic PHP script.

First, we need a script to go over, so here is a very simple script:

<html>
<head>
<title>My first PHP script</title>
</head>
<body>
<?php
echo 3 + 2;
?>
</body>
</html>

This is a very simple script. If you run this script, the output will be 5.

First, this part:

<html>
<head>
<title>My first PHP script</title>
</head>
<body>

If you have ever done HTML before you will notice that this part of out page is just HTML.

The next part which is very important is this:

<?php

This part of the script tells the PHP parser that after this line there is a PHP script. This way, the parser knows to parse whatever is inside the tag before the output is sent to the users web browser.

Here is the next part:

echo 3 + 2;

This is the line that creates the actual output of the number 5. First, you will see the word "echo". Echo is a PHP language construct. It is basicaly a function that can output text or an expression. In this case, we tell echo to output the sum of 3 and 2. So 3 + 2 = 5, and the echo statement outputs the 5. At the very end of the line you will see a ";". This tells the PHP parser that this is the end of the line. You MUST have a ; at the end of each line, although a statement can span a number of lines, at the end of that statement there must be a ";". We could have done something like:

echo 'This is some text';

As you would expect, that would have outputed "This is some text" to the page.

Lets continue through our script. The next line is:

?>

That is the closing PHP tag. It tells the PHP parser that the PHP code ends there. Although it tells the parser that script is done, you can have as many <?php ?> tags on one page as you want.

The next two lines are:

</body>
</html>

These two lines are just simple HTML tages. First the body of the page is ended, then the HTML page is ended.

THIS TUTORIAL IS CONTINUED BELLOW


BBS Signature
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Beginners tutorial 2005-11-03 23:42:32 Reply

Chapter 4 :: Using variables

One of the most important aspects of PHP, and any programming language is variables. Variables can hold strings (text) and/or numbers.

A PHP script using variables would look like this:

<html>
<head>
<title>PHP using variables</title>
</head>
<body>
<?php
$number1 = 5;
$number2 = 6;
$sum = $number1 + $number2;

echo $sum;
?>
</body>
</html>

This script is very much like the script we did before. In PHP, to use a variable you do: $myvariable. You can put a dollar sign before any word to make it a variable. Here are some conditions for variable naming. Variables can have underscores(_), and can have capital and lowercase letters. Variables cannot have spaces.

Back to the script above. We create 3 variables in this script. First, we create a variable called number1. We set its value to 5. We then create a variable called number2. We set that variables value to 6. We then have a variable called sum. We set the value of the variable sum to $number1 + $number2. Because $number1 = 5, and $number2 = 6, this is like saying $sum = 5 + 6. So after that line is parsed, the value of $sum is 11.

In PHP, variables are not explititly typed. What that means is that you do not tell PHP what type of variable to create (string or number). PHP automatically knows what kind of variable to make it. Lets say you want to make a variable to hold a string value, heres what it would look like:

<?php
$mytext = 'This is my string variable';
?>

In that example, you will notice that the value of $mytext is enclosed within a single quote (') and a closed by a single quote ('). This declares a string variable. When ever you are creating a variable that will just hold text, you must always inclose it within double quotes (") or single quotes (').

Right now you're probably asking what the difference is if you quote a string variable with double quotes, which would look like this:

<?php
$mytext = "This is a test";
?>

Or with single quotes, which would look like this:

<?php
$mytext = 'This is a test';
?>

The fact is there is very little difference. If you try these examples, both will work perfectly. With that said, for this example, one was is correct and one way is wrong. When you are creating a variable that will just hold text, and will not have any variables within it, you should always use single quotes. When PHP sees statements with single quotes, is means there are no variables in it, therefore the PHP parser does not try to interpret variables within the statement, and the page is parsed a tiny bit faster.

Here are some examples of when you would use sinngle quotes and when it would be correct to use double quotes.

Here is the first example:

<?php
$mytext = 'This is just text, and there is no variable here.';
?>

In the example above, it is ok to use single quotes because the string is just plain text. Using double quotes in the example above would just cause the page to parse a tiny bit slower.

Here is another example"

<?php
$name = 'David';
$greeting = "Welcome to my site $name";
echo $greeting;
?>

This is an interesting example. You will first notice that the value of the $name variable is enclosed in single quotes. This is because there is only text in that variable, therefore it is the proper way to do it. Then you will notice that the contents of the $greeting variable are enclosed in double quotes. This is proper because within the value of $greeting, you are supplying a variable (the $name variable.). So the value of greeting becomes "Welcome to my site David", and that will display on the page if you try this example. If you put single quotes around the contents of $greeting, the output of the page will be "Welcome to my site $name". This is because everything within single quotes is interpreted as plain text.

Chapter 5 :: Using some basic conditional statements

In this chapter, you will learn some very important features of PHP, conditional statements. Conditional statements allow you to add logic to PHP scripts.

One of if not the most used conditional statement is called the "if" statement. The if statement allows you to say "If something is true, do this, else do this."

Here is the outline of an if statement:

if ([condition])
{
[do everything in here if condition is true]
}else{
[do everything in here if condition is false]
}

To better show how the if statement works, here is an actualy PHP example. Lets say we have a variable called name, and we want to see if that name is equal to "David". If the value of the name variable is David, we want the script to output "The name is David", and if the value of the name variable is not David, we want it to output "This name is not David". For this example, we will make the name variable equal to "David", so our condition will be true.

Here's how we would do this:

<html>
<head>
<title>What is the name?</title>
</head>
<body>
<?php
$name = 'David';

if ($name == 'David')
{
echo 'The name is David';
}else{
echo 'The name is not David';
}
?>
</body>
</html>

This example is pretty simple. To start the script off, we make the value of the $name variable equal to "David". We then start the if statement. We say "If the value of the name variable is equal to David, do the follwing". You will notice that it says $name == 'David'. In PHP, to check for equality, you use ==. If you use =, it will not work right because = sets things to different values. In this case, the condition is true, so the line:

echo 'The name is David';

is executed. If the name variable was not equal to David, the else would have came into play, and this line would have been executed:

echo 'The name is not David';

That is the end of this tutorial. Post if you have any questions.


BBS Signature
DariusR
DariusR
  • Member since: Dec. 24, 2004
  • Offline.
Forum Stats
Member
Level 16
Animator
Response to PHP: Beginners tutorial 2005-11-04 00:02:39 Reply

Quick Question:

You can also use Javascript AND PHP at the same time right? Just tryin' to make sure. Awsome tutorial. I gotta bookmark it on my browser. XD


Kill a man, you're a murderer, kill many, you're a conqueror, kill 'em all, and you're a god!

BBS Signature
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Beginners tutorial 2005-11-04 00:05:24 Reply

Thanks, I'm glad you like it!

Yes, using PHP and JavaScript together can be very powerful. So just to answer your question, you can easily use them together.


BBS Signature
blah569
blah569
  • Member since: Jan. 18, 2005
  • Offline.
Forum Stats
Member
Level 25
Programmer
Response to PHP: Beginners tutorial 2005-11-04 20:12:58 Reply

WOW! Very baisc for begginers to learn from!


BBS Signature
darc10222
darc10222
  • Member since: Nov. 3, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to PHP: Beginners tutorial 2005-11-04 20:49:50 Reply

Woh. Im going into Flash right now to try it out.

<deleted>
Response to PHP: Beginners tutorial 2005-11-07 18:23:47 Reply

At 11/4/05 08:49 PM, darc10222 wrote: Woh. Im going into Flash right now to try it out.

Flash? PHP is a programming language used on the web.

Jessii
Jessii
  • Member since: Feb. 10, 2005
  • Offline.
Forum Stats
Member
Level 36
Movie Buff
Response to PHP: Beginners tutorial 2005-11-07 18:47:08 Reply

At 11/7/05 06:23 PM, PantherLock wrote: Flash? PHP is a programming language used on the web.

Yes, for webpages, not flash.

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: Beginners tutorial 2005-11-07 19:45:07 Reply

Instead of just saying php pages end in .php you should mention the other extensions that php work with as well. If they're running php3 they'll need the .php3 extension, etc.

theY4Kman
theY4Kman
  • Member since: May. 25, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to PHP: Beginners tutorial 2005-11-07 20:01:05 Reply

How do you echo variables that are given in the browser URL? e.g. http://y4kstudios.com/blah.php&text='fun!'

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: Beginners tutorial 2005-11-07 22:13:53 Reply

You would use the $_GET superglobal. Oh yeah, URL variables' context do not have enclosed in quotes.

Gylfi
Gylfi
  • Member since: Aug. 20, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to PHP: Beginners tutorial 2006-01-27 09:00:11 Reply

<html>
<body>
<?php
echo 'Hello world!';
?>
</body>
</html>

Lets say we saved that page as test.php. Here is the chain of events that would happen when that page is viewed

It doesnt happen when i try it, and even before i opened this page i saved as test.php, add me to MSN at gylfi@stebbi.is. I may need help.

<html>
<head>
<title>My first PHP script</title>
</head>
<body>
<?php
echo 3 + 2;
?>
</body>
</html>

This is a very simple script. If you run this script, the output will be 5.

No, the output is the script itself, i cant run it :(

First, this part:

<html>
<head>
<title>My first PHP script</title>
</head>
<body>

If you have ever done HTML before you will notice that this part of out page is just HTML.

No i have not done HTML before, i don't notice anything.

The next part which is very important is this:

<?php

This part of the script tells the PHP parser that after this line there is a PHP script. This way, the parser knows to parse whatever is inside the tag before the output is sent to the users web browser.

The output isn't sent to te browser, the script is.

Again, Add me to your Hotmail account at gylfi@stebbi.is

Here is the next part:
echo 3 + 2;

could have done something like:

echo 'This is some text';
As you would expect, that would have outputed "This is some text" to the page.

[above] :(

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to PHP: Beginners tutorial 2006-01-27 10:02:39 Reply

At 1/27/06 09:00 AM, M4TR1X wrote: No, the output is the script itself, i cant run it :(

You don't have php installed, get a host that does

Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to PHP: Beginners tutorial 2006-01-28 11:55:39 Reply

At 1/27/06 09:00 AM, M4TR1X wrote:
Lets say we saved that page as test.php. Here is the chain of events that would happen when that page is viewed
It doesnt happen when i try it, and even before i opened this page i saved as test.php, add me to MSN at gylfi@stebbi.is. I may need help.
As you would expect, that would have outputed "This is some text" to the page.
[above] :(

why waste our time by quoting and commenting on every sentence, just say, IT DOESN'T WORK FOR ME, can someone help?

EviLDoG
EviLDoG
  • Member since: Oct. 18, 2000
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to PHP: Beginners tutorial 2006-01-28 12:10:52 Reply

At 1/28/06 11:55 AM, Jams44 wrote: why waste our time by quoting and commenting on every sentence, just say, IT DOESN'T WORK FOR ME, can someone help?

Don't be too harsh, he's new to PHP :). When i first tried installing a script and see alot of code i wondered what the fuck had happened.

Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to PHP: Beginners tutorial 2006-01-28 22:26:02 Reply

lol but couldn't he say, its not working, its as simple as that!

Ziaja
Ziaja
  • Member since: Oct. 2, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to PHP: Beginners tutorial 2006-01-29 00:56:03 Reply

At 11/4/05 08:49 PM, darc10222 wrote: Woh. Im going into Flash right now to try it out.

Really, good luck with that.

Chapter 1 :: What PHP can be used for

PHP can be used to create interactive web applications. PHP can allow you to easily create simple web applications, or with a little more knowledge you can create massive, feature rich we applications.

You should have mentioned how its shown. But other than that, really nice tutorial, i'll be refering people to this.


Obnoxious2ombie.com - Online Drug Lord!

citricsquid
citricsquid
  • Member since: Jun. 25, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to PHP: Beginners tutorial 2006-02-25 08:44:43 Reply

Thankyou this is the first thing i have looked at on using php and i now understand the basics, i am very grateful for this tutorial and also you are a great teacher.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Beginners tutorial 2006-02-25 14:10:49 Reply

At 2/25/06 08:44 AM, onnet092 wrote: Thankyou this is the first thing i have looked at on using php and i now understand the basics, i am very grateful for this tutorial and also you are a great teacher.

Aww thanks for the kind words. It means a lot. I'll try to make some new tutorials soon.


BBS Signature
citricsquid
citricsquid
  • Member since: Jun. 25, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to PHP: Beginners tutorial 2006-02-25 16:07:06 Reply

At 2/25/06 02:10 PM, GamesCool wrote:
At 2/25/06 08:44 AM, onnet092 wrote: Thankyou this is the first thing i have looked at on using php and i now understand the basics, i am very grateful for this tutorial and also you are a great teacher.
Aww thanks for the kind words. It means a lot. I'll try to make some new tutorials soon.

Hey np, i am looking out for more tuorials by you. Anyway if anyone would like to help me with my site please just add me on msn (s4mr@hotmail.co.uk)

humbug88
humbug88
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to PHP: Beginners tutorial 2006-04-25 09:46:09 Reply

Hmmm....

I can't get it to work! Let me get this strait. You save the whole file (including the html stuff) as a ".php" ? I did that and when I opened it nothing happened. Nothing at all. It didn't open. I opened it as a html extention and it only showed the html stuff (suprise, suprise).

I know the whole concept of php now thanx to the greate tut but i need help on gettin git to work! reply here or email me (in profile).

All help is greatly apreciated :).

humbug


BBS Signature
Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: Beginners tutorial 2006-04-25 11:01:17 Reply

Nice little tutorial. I think you should make a more advanced one for people who want to know the full background on PHP. Such as how variable memory is allocated, how the function libraries work, etc.

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to PHP: Beginners tutorial 2006-04-25 11:41:37 Reply

At 4/25/06 11:01 AM, Pilot-Doofy wrote: Nice little tutorial. I think you should make a more advanced one for people who want to know the full background on PHP. Such as how variable memory is allocated, how the function libraries work, etc.

It is not small , it is basic. It is good. Is is by GamesCool. Haha!

NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to PHP: Beginners tutorial 2006-04-25 11:55:14 Reply

At 11/7/05 08:01 PM, theY4Kman wrote: How do you echo variables that are given in the browser URL? e.g. http://y4kstudios.com/blah.php&text='fun!'

As the Pilot said, $_GET superglobals.

Like that:
<?
//initialize a super global variable
$a = $_GET['text'];
printf ("It says %.s", $a);
// I've heard it is faster than echo but if
// you want to use echo do like this:
echo "It says $a";
// and you don't have to put a message
// in a string too but I did so to make it more
// understandful
?>

Here's another example, how to link to those superglobals
<?
$a = $_GET['text'];
echo '[a href="?text='.$a.'"]Link[/a]';
echo "\n";
?>

It would link to the selected page AND would have the message and the super global variable included in it.

Here's how you would enter some message and then display it in the same doocument via $_GET

<?
ob_start();
$i = $_GET['message'];

//check if processed
if (isset($_POST['do_submit'])) {
// proccess it
$a = $_POST['something']:

if (empty($a)) {
echo "You've left your message empty. \n";
die();
}
$a = htmlentities($a);

// this could be done probably faster too
header ("location: ?message=$a");

// not let's just output it
echo "<strong>You has written</strong>: \n";
echo "<br /> \n";
echo $a;
echo "<br /> \n";

} else {
//not proceseed
echo '
<form name="someForm" method="POST" action="'.$PHP_SELF.'">
<textarea name="something" cols="10" rows="10"></textarea>
<input type="submit" name="do_submit" value=" Submit ! " />
</form>
';
}
ob_end_flush();
?>

humbug88
humbug88
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to PHP: Beginners tutorial 2006-04-25 22:33:04 Reply

At 4/25/06 02:35 PM, blackvector wrote: You need a server ON THE WEB that is PHP compatible.

Ok, thanks.

So i cant test it unless i actualy put it on the web? I have php installed on my computer so I was just wondering.

humbug


BBS Signature
harryjarry
harryjarry
  • Member since: May. 15, 2005
  • Offline.
Forum Stats
Member
Level 56
Blank Slate
Response to PHP: Beginners tutorial 2006-04-25 22:45:23 Reply

At 4/25/06 10:33 PM, humbug88 wrote: So i cant test it unless i actualy put it on the web? I have php installed on my computer so I was just wondering.

humbug

Yes you can, download this and then you can debug it and it will do the php for you instead of always having it online.

A great tool anyway, you just have to fill out the form first but it is short.


twitter | last.fm // Currently working on HarryJarry.com

BBS Signature
humbug88
humbug88
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to PHP: Beginners tutorial 2006-04-26 04:44:25 Reply

At 4/25/06 10:45 PM, harryjarry wrote: A great tool anyway, you just have to fill out the form first but it is short.

thanks


BBS Signature
Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to PHP: Beginners tutorial 2007-04-02 17:53:24 Reply

Sorry for giant bump(I doubt I will get a response) but I have a question

I have 3 Strings and I'm wondering if there's a way to echo them if a form button is pushed...

I have no knowledge of PHP except for what you said in this tutorial(it's great btw) and I would just think it would be something in an if,maybe something like..

<input name="Mov1" type="button" value="Movie 1?" />
<input name="Mov2" type="button" value="Movie 2?" />
<input name="Mov3" type="button" value="Movie 3?" /><Br /><br />
<?php
$nothing='Nothing is being viewed'
$movie1='Viewing Movie Number 1'
$movie2='Viewing Movie Number 2'
$movie3='Viewing Movie Number 3'
if(Mov1.press)
{
echo $mov1
}
else
{
echo $nothing
}
?>

Of course I would have no clue,so is there any answer?Help is greatly appreciated.


wat
a compessor wil raise the volume while lowering the db - chronamut

RPGBandit
RPGBandit
  • Member since: Nov. 23, 2006
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to PHP: Beginners tutorial 2007-04-02 18:00:35 Reply

by FAR one of my favorite tutorials, so easy to understand for beginners! Excellent DFox :D

eWhabs
eWhabs
  • Member since: May. 4, 2006
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to PHP: Beginners tutorial 2007-04-02 19:11:53 Reply

At 4/2/07 05:53 PM, Thomas wrote: I have 3 Strings and I'm wondering if there's a way to echo them if a form button is pushed...

If you want it to be done without reloading the page, you have to use Javascript.


-Disregard females (fuck bitches)
-Acquire currency (get money)
-Ignite cannabis frequently (smoke sum with your homies)

BBS Signature