hopzone
10-06-2006, 04:33 PM
This tutorial will take you through the basics of PHP, and by the end of it you will be able to create small and simple PHP scripts.
PHP is a powerful language, which its main aim is to create dynamic web pages, which is what Blam Forums is built on, you will notice that all threads within Blam Forums are all from showthread.php, but each one has different content, this is a dynamic web page.
But before you learn how to create dynamics within PHP you must first learn some of its basic rules, such as syntax, which is best translated into grammer. Like all languages, english, french, german etc, you have certain ways of writing sentances, using grammer. Syntax is the same, there are certain ways to write PHP, otherwise it is wrong and wont work.
Step one would be to know the PHP tags, tags like HTML tags, shown below:
PHP Code:
<?php
?>
Anything inbetween those tags is treated as PHP and is executed, anything outside them is treated as HTML. But don't forget that your php files need to have the extension ".php".
The next step is to learn about variables, also known as "vars", these are best described as temporary memory, they can contain huge amounts of data, and stored in a small name. Lets look at how we would declare a variable:
PHP Code:
$MyVar = "Hello World!";
Okay, several things to take note of here, firstly the variable name, which is "$MyVar", and yes, the doller sign needs to be infront of every declared variable. The variable can be alphanumeric, with underscores, but not with spaces.
Next we are putting an equals sign after the variable, so show that we are "declaring" it, or in simple terms, putting data into it, also known as a "string" (ie: "String Of Data"). After that we put down what we are storing within the variable, which is "Hello World", due to this been text we need to put quotes around it, so PHP knows its a string of data and doesn't treat it as PHP syntax.
After the quotes we have a ;, this is part of PHP syntax, about 95% of PHP needs this at the end of a line, its best to think of it as a full stop. If you don't put it in, you can get all kinds of errors ;)
So now you know how to declare a variable, but i told you that PHP could make pages dynamic, and you're probably wondering how do i output data on the page.
Outputting data onto a page requires to use of a command, known as echo, this will output anything to the webpage that you specify. An example of it is shown below:
PHP Code:
echo "Outputting Data";
As you can see the syntax applies here, but there is no equals sing because we arn't declaring a variable, we are using a command.
The above example ouputs data, but what if you wanted to output data from a variable? This can be done too, shown below:
PHP Code:
echo $MyVar;
Notice how there is no quotes this time, because it is a variable and not plain text, so the PHP will swap the variable with its contents. (The quotes are delcared when we set the variable)
Now you know how to ouput plain text and variables, the next step is to output them both together.
PHP Code:
$Name = "Blaminator";
echo "Good Morning " . $Name . ", There are 10000 new posts.";
Here we have quotes around the plain text, but when it comes to putting a variable in, we put in an ending quote, but then a fullstop, PHP treats the fullstop as a joiner as such, it will join the 2 things together, or in this case 3 things, as we have "PlainText Variable PlainText".
Now you will see how PHP can be dynamic, using an "If Conditional", which is designed to check if something is true or false, and depending on the result you can execute different code.
An example If Conditional is shown below:
PHP Code:
$Name = "Blaminator";
if($Name == "Blaminator"){
echo "Your Name Is Blaminator!";
} else {
echo "Your Name Is Not Blaminator.";
}
It may look like its crazy complicated, but it is really simple when looked at in a different perspective, i will re-write the above code in "pseudo-code", this is half-code, half english, used to give you guys a better understanding.
[php]
$Name = "Blaminator";
If $Name Equals Blaminator Then
echo "Your Name Is Blaminator";
Else
echo "Your Name Is Not Blaminator.";
End If
Read that code outloud(http://www.blamforums.com/forum/images/smilies/cornlol.gif), and you will see it is very simple. But getting back to the propper If Conditional, a couple of syntax rules.
The if conditional uses { brackets, anything inbetween these are what is executed depending on which side of the statement is on (if its true or false).
These brackets dont have a ; after them, it isn't required. And lastly you will notice that the statement has 2 equal signs instead of 1, this must be applied.
Building onto the If Conditional, it can have several other conditions joint onto it, an "Else-If Statement", an example of one of those is shown below, but it doesn't need explaining:
PHP Code:
$Name = "Blaminator";
if($Name == "Blaminator"){
echo "Your Name Is Blaminator!";
} else if($Name == "Bob"){
echo "Your Name Is Bob!";
} else if($Name == "Bill"){
echo "Your Name Is Bill!";
} else {
echo "Who are you?";
}
Next, functions, also known in other programming languages as routines. Lets look at how to construct a function:
PHP Code:
function FUNCTIONNAME(PASSED-VARS){
}
The function name, like variables has to be aplphanumeric, and can contain underscores, with no spaces, however a doller sign is not needed here.
The passed-vars is an area where variables can be sent into the function when its called, we will look at this after a little more on functions.
Here is a basic function, doesn't do much, but its a function:
PHP Code:
function pastedata()
{
echo "Hello guys";
}
Now, if you put that in a php file you would fine that Hello World would NOT appear, any code within functions is not executed unless the function is called, so lets see how to call this function:
PHP Code:
pastedata();
Jjust put the function name, the brackets, and ; at the end, the function will be executed. Now lets look into those passed-vars i was talking about earlier.
PHP Code:
function AddNumbers($Num1, $Num2){
$Answer = $Num1 + $Num2;
echo $Num1 . " + " . $Num2 . " = " . $Answer;
}
Note that the variables "$Num1" and "$Num2" do NOT need to be declared, these are just names given for when the function is called, calling this function is shown below:
PHP Code:
AddNumbers(4,5);
So when the function is called, it stores 4 in $Num1, and 5 in $Num2, just as names for the function to use within its code. The function has a minor maths equation, but nothnig that would trouble you.
Basic Mathmaticle operators are: + (Plus/Addition), - (Minus/Take-Away), * (Times/Multiply), / (Divide)
If that all makes sense to you then you will be able to tell the output on that page would be: "4 + 5 = 9" (Without the quotes)
So whats the point in functions, well if you wanted to execute a math equation, like in the function above, but several times, it would be a pain to just keep writing the same equation over and over, but with that function, i can just call the function several times over, less writing, less code, and more effecient.
PHP is a powerful language, which its main aim is to create dynamic web pages, which is what Blam Forums is built on, you will notice that all threads within Blam Forums are all from showthread.php, but each one has different content, this is a dynamic web page.
But before you learn how to create dynamics within PHP you must first learn some of its basic rules, such as syntax, which is best translated into grammer. Like all languages, english, french, german etc, you have certain ways of writing sentances, using grammer. Syntax is the same, there are certain ways to write PHP, otherwise it is wrong and wont work.
Step one would be to know the PHP tags, tags like HTML tags, shown below:
PHP Code:
<?php
?>
Anything inbetween those tags is treated as PHP and is executed, anything outside them is treated as HTML. But don't forget that your php files need to have the extension ".php".
The next step is to learn about variables, also known as "vars", these are best described as temporary memory, they can contain huge amounts of data, and stored in a small name. Lets look at how we would declare a variable:
PHP Code:
$MyVar = "Hello World!";
Okay, several things to take note of here, firstly the variable name, which is "$MyVar", and yes, the doller sign needs to be infront of every declared variable. The variable can be alphanumeric, with underscores, but not with spaces.
Next we are putting an equals sign after the variable, so show that we are "declaring" it, or in simple terms, putting data into it, also known as a "string" (ie: "String Of Data"). After that we put down what we are storing within the variable, which is "Hello World", due to this been text we need to put quotes around it, so PHP knows its a string of data and doesn't treat it as PHP syntax.
After the quotes we have a ;, this is part of PHP syntax, about 95% of PHP needs this at the end of a line, its best to think of it as a full stop. If you don't put it in, you can get all kinds of errors ;)
So now you know how to declare a variable, but i told you that PHP could make pages dynamic, and you're probably wondering how do i output data on the page.
Outputting data onto a page requires to use of a command, known as echo, this will output anything to the webpage that you specify. An example of it is shown below:
PHP Code:
echo "Outputting Data";
As you can see the syntax applies here, but there is no equals sing because we arn't declaring a variable, we are using a command.
The above example ouputs data, but what if you wanted to output data from a variable? This can be done too, shown below:
PHP Code:
echo $MyVar;
Notice how there is no quotes this time, because it is a variable and not plain text, so the PHP will swap the variable with its contents. (The quotes are delcared when we set the variable)
Now you know how to ouput plain text and variables, the next step is to output them both together.
PHP Code:
$Name = "Blaminator";
echo "Good Morning " . $Name . ", There are 10000 new posts.";
Here we have quotes around the plain text, but when it comes to putting a variable in, we put in an ending quote, but then a fullstop, PHP treats the fullstop as a joiner as such, it will join the 2 things together, or in this case 3 things, as we have "PlainText Variable PlainText".
Now you will see how PHP can be dynamic, using an "If Conditional", which is designed to check if something is true or false, and depending on the result you can execute different code.
An example If Conditional is shown below:
PHP Code:
$Name = "Blaminator";
if($Name == "Blaminator"){
echo "Your Name Is Blaminator!";
} else {
echo "Your Name Is Not Blaminator.";
}
It may look like its crazy complicated, but it is really simple when looked at in a different perspective, i will re-write the above code in "pseudo-code", this is half-code, half english, used to give you guys a better understanding.
[php]
$Name = "Blaminator";
If $Name Equals Blaminator Then
echo "Your Name Is Blaminator";
Else
echo "Your Name Is Not Blaminator.";
End If
Read that code outloud(http://www.blamforums.com/forum/images/smilies/cornlol.gif), and you will see it is very simple. But getting back to the propper If Conditional, a couple of syntax rules.
The if conditional uses { brackets, anything inbetween these are what is executed depending on which side of the statement is on (if its true or false).
These brackets dont have a ; after them, it isn't required. And lastly you will notice that the statement has 2 equal signs instead of 1, this must be applied.
Building onto the If Conditional, it can have several other conditions joint onto it, an "Else-If Statement", an example of one of those is shown below, but it doesn't need explaining:
PHP Code:
$Name = "Blaminator";
if($Name == "Blaminator"){
echo "Your Name Is Blaminator!";
} else if($Name == "Bob"){
echo "Your Name Is Bob!";
} else if($Name == "Bill"){
echo "Your Name Is Bill!";
} else {
echo "Who are you?";
}
Next, functions, also known in other programming languages as routines. Lets look at how to construct a function:
PHP Code:
function FUNCTIONNAME(PASSED-VARS){
}
The function name, like variables has to be aplphanumeric, and can contain underscores, with no spaces, however a doller sign is not needed here.
The passed-vars is an area where variables can be sent into the function when its called, we will look at this after a little more on functions.
Here is a basic function, doesn't do much, but its a function:
PHP Code:
function pastedata()
{
echo "Hello guys";
}
Now, if you put that in a php file you would fine that Hello World would NOT appear, any code within functions is not executed unless the function is called, so lets see how to call this function:
PHP Code:
pastedata();
Jjust put the function name, the brackets, and ; at the end, the function will be executed. Now lets look into those passed-vars i was talking about earlier.
PHP Code:
function AddNumbers($Num1, $Num2){
$Answer = $Num1 + $Num2;
echo $Num1 . " + " . $Num2 . " = " . $Answer;
}
Note that the variables "$Num1" and "$Num2" do NOT need to be declared, these are just names given for when the function is called, calling this function is shown below:
PHP Code:
AddNumbers(4,5);
So when the function is called, it stores 4 in $Num1, and 5 in $Num2, just as names for the function to use within its code. The function has a minor maths equation, but nothnig that would trouble you.
Basic Mathmaticle operators are: + (Plus/Addition), - (Minus/Take-Away), * (Times/Multiply), / (Divide)
If that all makes sense to you then you will be able to tell the output on that page would be: "4 + 5 = 9" (Without the quotes)
So whats the point in functions, well if you wanted to execute a math equation, like in the function above, but several times, it would be a pain to just keep writing the same equation over and over, but with that function, i can just call the function several times over, less writing, less code, and more effecient.