Monday, May 11, 2009

PHP - 01 - Introduction from Siforia on Vimeo.

Friday, November 28, 2008

Lesson 7 : More Loops

Objective
This lesson will take us further into the world of If/Else Statements and While
Loops. We will use Forms to collect information from our users, and then use
If/Else Statements and While Loops to process that data.

Step 2
Type in the lines just as they appear in text given below:

form action="”lesson7output.php”" method="”POST”">Choose an Animal:
select name="”animal”"> option>Bird /select
input type="”submit”">
/form

Note: This code is strictly HTML. Do not include the line at the end of this file. Type only what appears in the box above. Do
not copy-and-paste the code.


Step 3
Name this file lesson7input.php and save it in your Project Folder.

Step 4
Open a new window in your Text Editing Program. This will be used to create the
second file for this lesson.

Step 5
In the second file, type in the lines just as they appear in text given below:



Step 6
Name this file lesson7output.php and save it in your Project Folder.

Step 7
Upload lesson7input.php and lesson7output.php to your server.

Step 8
View the first file (www.YourWebDomain.com/lesson7input.php) in your Web
Browser. You should see a text field that asks for your age.

Step 9
Choose an animal from the dropdown menu and press the Submit Button.

Step 10
You should see a new page that displays a message. Depending on the animal
that you chose, the If/Else Statements will determine what message to display.


We just created two files. The first file is a form that allows you to choose an
animal. After you click the Submit Button, you are directed to the second file. The
second file uses If/Else Statements to determine what messages to display on
the screen.
First File (lesson7input.php)
Line 1




This line of code is the beginning of the form.
The portion that says action=”lesson7output.php” tells the form to forward the
browser to the file that is named “lesson7output.php” after the user clicks the
Submit Button.

First File (lesson7input.php)
Line 2


Choose an Animal:

This line of code is a simple text output to the screen.

First File (lesson7input.php)
Line 3


select name="”animal”" This line of code begins the dropdown box. The portion that says name=”animal” tells the form to create a variable named $animal. When you click the Submit Button, the form passes the variable to the next page. First File (lesson7input.php) Line 4 – Line 7 These lines of code each create an option in the dropdown box. When you choose one of these options from the dropdown box, the Form assigns that value to the $animal variable. First File (lesson7input.php) Line 8 /select

This line of code ends the dropdown box.

First File (lesson7input.php)
Line 9


input type="”submit”"

This line of code creates the Submit Button. When this button is clicked, it will tell
the form to process the data that has been entered.

First File (lesson7input.php)
Line 10




This line of code is the end of the form.

Note: We have examined all the code from lesson7input.php . We will now
examine the code from lesson7output.php .

Second File (lesson7output.php)
Line 2


$animal = $_POST[“animal”];


This line of code is used to define the variable $animal. This code uses the
$_POST function to get the value for the variable $animal, which was sent from
the from on the previous page.

Second File (lesson7output.php)
Line 3


if ($animal == ‘Dog’) {

This line of code uses an If Statement to test the variable $animal . The value of
$animal is decided by the value that you chose from the dropdown menu.
If the value of $animal is equal to ‘Dog’, then this If Statement is executed.
If the value of $animal is not equal to ‘Dog’, then the script continues to the next
test.

Second File (lesson7output.php)
Line 4


echo “Dog says WOOF”;

This line of code uses an echo statement to print “Dog says WOOF” to the
screen.

Note: The If/Else statements each test for one of the animals. If the test is found
to be true, that If/Else Statement is executed. Once an If/Else Statement is
executed, no more If/Else Tests are run.


Exercise 7-1
You can use a form to send more than one variable at a time. Here we will create
two files that will be used to send and receive multiple variables. The second file
will be use If/Else Statements and While Loops to create an output.

Name this file “lesson71input.php”


form action="”lesson7output.php”" method="”POST”">
Choose an Animal:
select name="”animal”"> option>Bird /select



# of Noises:
input type="”text”" size="”2”" name="”noises”">



input type="”submit”">
/form>


Name this file “lesson71output.php”

’;
$speak = ‘WOOF!’;
}
else if ($animal == ‘Cat’) {
echo ‘Cat says MEOW
’;
$speak = ‘MEOW!’;
}
else if ($animal == ‘Bird’) {
echo ‘Bird says TWEET
’;
$speak = ‘TWEET!’;
}
else if ($animal == ‘Mouse’) {
echo ‘Mouse says SQUEEK
’;
$speak = ‘SQUEEK!’;
}
else {
echo ‘There was an ERROR
’;
}
while ($noises > 0) {
echo $speak, ‘
’;
$noises = $noises – 1;
}
?>



Conclusion

In Lesson 7 we used a form to collect information from the user, and then used
If/Else Statements and While Loops to produce an output.

In reality, the examples that have been presented to you in this eBook are not
practical or useful in the real world, but they will provide you with the groundwork
to build your PHP skills. You will soon find yourself creating dynamic websites
that interface with visitors and databases.

I hope you have found this Blog to be a helpful resource. If you enjoyed this and would like to see me produce more about advanced PHP Programming, please feel free to contact me or leave your opinion msgs here.

Lesson 6 : While Loops

Objective
This lesson will teach you how to use While Loops to count and process data. A
While Loop can be used to count down or count up to a certain point.

Step 1
Open a new window in your Text Editing Program.
Step 2
Type in the lines just as they appear in text given below:


$x = 1;
while ($x < 5) {
echo ‘Current Value: ‘, $x , ‘
’;
$x = $x + 1;
}


Step 3
Name this file while.php and save it in your Project Folder.
Step 4
Upload while.php to your server.
Step 5
View the page (www.YourWebDomain.com/while.php) in your Web Browser. You
should see a display similar to what appears in text given below:


Current Value: 1
Current Value: 2
Current Value: 3
Current Value: 4
Current Value: 5


We just created a file that creates a variable $x and then uses a While Loop to
display and manipulate the value contained in variable $x.
When the While Loop begins, $x is equal to 1. The While Loop executes until $x
has a value that is greater than 5.

Line 2

$x = 1;

This line creates the variable $x and gives it a value of 1.

Line 3

while ($x < 5) {

This line begins the While Loop. The code between the parenthesis is the Test.
The code between the brackets will execute, and then return back to the Test. If
the Test is still valid, the code between the brackets will execute again.

Line 4

echo ‘Current Value: ‘, $x , ‘
’;

This is the first line of code that appears between the brackets. Since the Test
(line 3) is valid, the code between the echo statement is printed to the screen.

Line 5

$x = $x + 1;

This is the second line of code that appears between the brackets. This code
takes the current value of $x and adds 1 to that value. If the value of $x is
currently 1, the value of $x will be 2 after this line has been executed.


Note: If you do not increment the value of $x, the statement ($x < 5) will always be
true and the While Loop will continue to execute for an infinite number of times.
This can cause many problems.


Line 6

}

This line ends the While Loop. All While Loops must have a beginning and
ending bracket.

You can run multiple tests at once during a While Loop. To run multiple tests at
once you must use And/Or Operators.

When you use an OR Operator, the While Loop will execute as long as either of
the Tests is true. OR is represented by two pipe characters:

When you use an AND Operator, the While Loop will execute only as long as
both of the Tests are true. AND is represented by two ampersands: &&
And/Or Operators can also be used with If/Else Statements.

The AND/OR perators are listed in the table below:


Example Meaning

$x < 5 $y < 20 While $x is less than 5 OR $y is less than 20
$x < 5 && $y < 20 While $x is less than 5 AND $y is less than 20


In this exercise we will use an OR to test multiple variables at the same time.


$x = 1;
$y = 2;
while ($x < 5 $y < 20) {
echo ‘X: ‘, $x ,’ and Y: ‘, $y , ‘
’;
$x = $x + 1;
$y = $y + 2;
}
?>

Try changing the values of $x and $y and changing the values in the While Loop
to create different results.


Conclusion
In Lesson 6 we learned how to use While Loops to increment variables and
process data. While Loops will become more useful in the future but their
concepts are important to understand as we move forward.

In Lesson 7 we will more uses of While Loops and If/Else Statements. We will
learn how to integrate these with Forms to produce an interactive webpage.

Tuesday, November 25, 2008

Lesson 5 : If/Else Statements

Objective
This lesson will teach you how to use If / Else Statements to choose between two
options and to process data in different ways. Think of an If / Else Statement the
way you would think of a fork in the road. You come to the fork and you must
choose to go left or go right. When your program comes to an If / Else Statement,
the program will choose to go way or another.
Step 1
Open a new window in your Text Editing Program.
Step 2
Type in the lines just as they appear in the text below:


$x = 1;
if ($x == 1) {
echo “This is the If Statement “;
}
?>
Step 3
Name this file if.php and save it in your Project Folder.

Step 4
Upload if.php to your server.

Step 5
View the page (www.YourWebDomain.com/if.php) in your Web Browser. You
should see the words “This is the If Loop” in your Web Browser.

We just created a file that creates a variable $x and then uses an If Statement to
compare $x to the value of 1. Two equal signs are read as ‘is equal to’, so the
statement if ($x == 1) is read as ‘if $x is equal to 1’.
In this case, $x is equal to 1, so the program executes the code that is contained
within the brackets.
Line 2

$x = 1;
This line creates the variable $x and gives it a value of 1.

Line 3

If ($x == 1) {
This line begins the If Statement. The code between the parenthesis is the Test.
If the Test is true, then the code between the brackets will be executed.

Line 4

echo “This is the If Statement “;
This is the code that appears between the brackets. Since the Test (line 3) is
true, the code between the brackets is executed and the message ‘This is the If
Statement” is printed to the screen.

Line 5

}
This line ends the If Statement. All If Statements must have a beginning and
ending bracket.


Exercise 5-1
You can place an Else Statement after an If Statement. If the If Statement is not
true, the Else Statement will be executed instead. Here we will modify if.php to
contain an If / Else Statement.


$x = 1;
if ($x == 1) {
echo ‘This is the If Statement ’;
}
else {
echo ‘This is the Else Statement ‘;
}
?>


Try changing the value of $x to create a different result.


Exercise 5-2
Aside from ==, you can use many different tests within an If Statement. Some of
these tests are listed in the table below.


Test Meaning
$x > 1 If $x is greater than 1
$x < 1 If $x is less than 1
$x >= 1 If $x is greater than or equal to 1
$x != 1 If $x is not equal to 1


You also have the ability to use multiple If / Else Statements in succession, to
narrow down your choices. In this exercise we will use multiple If / Else
Statements.



$x = 1;
if ($x == 1) {
echo ‘X is equal to 1 ’;
}
else if ($x > 1) {
echo ‘X is greater than 1 ‘;
}
else if ($x < 1) {
echo ‘X is less than 1 ‘;
}
else {
echo ‘X is not a number ‘;
}
?>


Try changing the value of $x to create different results.


Conclusion
In Lesson 5 we learned how to use If / Else Statements to run different
operations, based on the value of a variable. If / Else Statements can be very
useful when determining what data to display on your screen, or what functions
to run. In the future we will collect information from users via a Form (Lesson 4)
and then use If / Else Statements to help process that data.
In Lesson 6 we will explore how to use While Loops to count and process data.

Lesson 4 : Forms

Objective
This lesson will teach you how to get information from the user, and then how to
output that information to the screen. For this lesson we will need to create two
separate files. The first file will contain an HTML form which will be used to
collect information from the user. The second file will be used to output the data
to the screen.

Step 1
Open a new window in your Text Editing Program.

Step 2
Type in the lines just as they appear in the text below:

form action="”form1output.php”" method="”POST”"
Enter Your Name:
input type="”text”" size="”10”" name="”username”"
input type="”submit”"
/form

Note: This code is strictly HTML. Do not include the line at the end of this file. Type only what appears in the box above, and
do not copy-and-paste the code.

Step 3
Name this file form1input.php and save it in your Project Folder.

Step 4
Open a new window in your Text Editing Program. This will be used to create the
second file for this lesson.

Step 5
In the second file, type in the code just as it appears in the text below:

Step 6
Name this file form1output.php and save it in your Project Folder.

Step 7
Upload form1input.php and form1output.php to your server.

Step 8
View the first file (www.YourWebDomain.com/form1input.php) in your Web
Browser. You should see a text field that asks for your name.

Step 9
Enter your name into the text field and press the Submit Button.

Step 10
You should see a new page that prints your name to the screen with a message
“Hello, YourName”.

We just created two files. The first file is a form that allows you to input your
name. After you click the Submit Button, you are directed to the second file. The
second file displays a message that uses the name that entered on the first
screen.

First File (form1input.php)
Line 1


form action="”form1output.php”" method="”POST”"

This line of code is the beginning of the form.
The portion that says method=”POST” tells the form how to process the data.
The portion that says action=”form1output.php” tells the form to display the file
that is named “form1output.php” after the user clicks the Submit Button.

First File (form1input.php)
Line 2


Enter Your Name:

This line of code is a simple text output to the screen.

First File (form1input.php)
Line 3


input type="”text”" size="”10”" name="”username”"

This line of code creates the text field. When you view this page in your Web
Browser, this line creates the area where you type your name.
The portion that says type=”text” tells the Form that this is a text input field.
The portion that says size=”10” tells the Form that the text field should be 10
characters in length.

The portion that says name=”username” creates a variable and associates it with
this text input field. In this case, the variable is named username. When you call
this variable in a PHP file, you will need to add a dollar sign to the beginning of
the name, so it will be named $username.

First File (form1input.php)
Line 4


input type="”submit”"

This line of code creates the Submit Button. When this button is clicked, it will tell the form to process the data that has been entered.

First File (form1input.php)
Line 5


/form

This line of code is the end of the form.

Note: We have examined all the code from form1input.php . We will now examine
the code from form1output.php .


Second File (form1output.php)

Line 2

$username = $_POST[“username”];

This line of code defines the $username variable for this second file. Since we
used action=”POST” to send the form from the first page, we will use the
$_POST function to retrieve the variable on this page. We must define the
$username variable in this fashion, before we can use it within this page of code.

Line 3

echo “Hello, “, $username;

This line of code takes the variable and outputs it along with some text.

You can use a form to send more than one variable at a time. Here we will create
two files that will be used to send and receive multiple variables.
Name this file “form2input.php”

form action="”form2output.php”" method="”POST”"
Enter Your Name:
input type="”text”" size="”10”" name="”username”"

Enter Your Age:
input type="”text”" size="”3”" name="“userage”"
input type="”submit”"
/form

Name this file “form2output.php”

You can use a form to send more than one variable, and then use the display
screen to process those variables. Here we will create two files that will send and
receive two numeric variables and then add them together before displaying the
result on your screen.
Name this file “form3input.php”

form action="”form3output.php”" method="”POST”"
First Number:
input type="”text”" size="”10”" name="”variable1”"

Second Number:
input type="”text”" size="”3”" name="“variable2”"
input type="”submit”"
/form

Name this file “form3output.php”


In Lesson 4 we learned how to use forms to collect information from the user and
to output that information to the screen. Forms require 2 files. The first file
displays the form and collects the data from the user. The second file defines the
variables with $_POST, processes the data and displays the processed data on
the screen.

In Lesson 5 we will explore how to use If / Else Statements to help us process
data more effectively.

Lesson 3 : Basic Mathematics

Objective
Now that you understand the basics of using variables, we’re going to use some
variables with numbers. We will create two variables that contain different
numbers, then we will create a third variable by adding the first two variables
together.

Step 1
Open a new window in your Text Editing Program.

Step 2
Type in the lines just as they appear in the text below:

$x = 1;
$y = 2;
$z = $x + $y;
echo “Answer: ”, $z;
?>


Step 3
Name this file addition.php and save it in your Project Folder.

Step 4
Upload addtion.php to your server.

Step 5
View the page (www.YourWebDomain.com/addition.php) in your Web Browser.
You should see the words “Answer: 3” in your Web Browser.


We just created a file that adds two variables together and then outputs the
answer to the screen. Now we’ll go line by line to help.

Line 2 and Line 3

$x = 1;
$y = 2;


These lines of code create two variables.
The first variable is named $x. We give this variable a value equal to “1”. When
you are using a numeric value, you do not need to put quotes around the value.
Our second variable is named $y. We give this variable a value equal to “2”.

Line 4

$z = $x + $y;

This line of code creates a variable named $z. Instead of typing the actual value
of $z, we create a value for $z by adding $x and $y. Since the variable $x holds
the value “1” and the variable $y holds the value “2”, the variable $z takes on a
value of “3”.

Line 5

echo “Answer: ”, $z;

This line of code uses the echo command, some text and our variable $z.
Whenever you want to display text on the screen, you use the echo command.
In Lesson 2, we learned that we can mix plain text and variables to create an
output. In this example, we use the text “Answer: “ and then display the variable
$z. When this displays on the screen, it shows “Answer: 3”.


You can use different values and different mathematical functions to create
different values and outputs. You can also output more than one variable at a
time. Try this example in the text below:

$x = 20;
$y = 5;
$z = $x - $y;
echo $x, “ - “, $y, “ = “, $z;
?>

Try giving your variables different values and try using different mathematical
functions to see what you can display on your screen.


Exercise 3-2
After a variable has been defined, you can redefine that variable by giving it a
new value. When you display a variable on the screen, it will display whatever
value it contains at that point in the code. Try this example in the text below:


$x = 60;
$y = 40;
$z = $x + $y;
echo “Answer 1: “, $z;
$x = 30;
$y = 20;
$z = $x - $y;
echo “Answer 2: “, $z;
?>


Conclusion
In Lesson 3 we learned how to use numeric variables and mathematical
functions to create new variables and how to display those variables in our Web
Browser.

In Lesson 4 we will explore how to make this more useful by getting input from
the user and using that input to generate an answer.

Lesson 2 : Variables

Objective

Now that we know how to print a simple message to our Web Browser, we’re
going to introduce variables. Using a variable is like using a notecard. You write a
name on the front of the notecard, then on the back of the notecard you write a
value. When you need to use that value again, you reference the notecard by the
name that you wrote on the front.


Step 1
Open a new window in your Text Editing Program.

Step 2
Type in the lines just as they appear in the text below:


$x = “Hello World”;
echo $x;
?>


Step 3
Name this file hello2.php and save it in your Project Folder.

Step 4
Open your FTP Program and connect to your server.

Step 5
Click on hello2.php in your FTP Program and drag hello2.php to the main
directory on your server. Allow a moment for the file to transfer.

Step 6
If you have completed all of these tasks correctly, you should be ready to view
your file online. Open your Web Browser, and using your web domain, type in
www.YourWebDomain.com/hello2.php. You should see the words “Hello World”
in your Web Browser.

The output seems to be identical to Lesson 1, but there is obviously something
different in the code. Now we’ll go line by line to help understand what we did.

Line 1


This will be the first line to every PHP file that you ever create. This tells the
server that the code (or section of code) that it is about to read is written in PHP.

Line 2

$x = “Hello World”;

This line of code creates a variable named $x. Variable must always start with a
dollar sign ($) and must not contain spaces or special characters. Variable
names must begin with a letter (x) and you may use underscores when naming
your variables.
In this example, we give our variable a value equal to “Hello World”. This means
that whenever we want to output “Hello World”, all we need to do is reference our
variable $x.
Whenever you define a value for a variable, you must enclose the value in
quotes and end the line with a semicolon. In this example we give our variable a
value of “Hello World”.

Line 3

echo $x;

This line of code uses the echo command and our variable $x. Whenever you
want to display text on the screen, you use the echo command. This time instead
of typing “Hello World” after our echo command, we simply use our variable $x.
When displaying a variable on the screen, it is not necessary to enclose the
variable in quotes. Since our variable $x contains the value of “Hello World”, the
file prints “Hello World” whenever we ask for variable $x.


Line 4
?>


This will be the final line to every PHP file that you ever create. This tells the
server that the current section of PHP code has come to an end.


Exercise 2-1
When using an echo statement, you can mix plain text with variables. To display
plain text with variables, you must put the plain text inside of quotes and separate
the plain text and variable with a comma. Try the example in the text below:


$x = “World”;
echo “Hello “, $x;
?>


Try different combinations of variables and plain text to see what you can display
on your screen.

Line 4
This will be the final line to every PHP file that you ever create. This tells the
server that the current section of PHP code has come to an end.

Exercise 2-1
When using an echo statement, you can mix plain text with variables. To display
plain text with variables, you must put the plain text inside of quotes and separate
the plain text and variable with a comma. Try the example in the grey box below:

Try different combinations of variables and plain text to see what you can display
on your screen.

Conclusion
In Lesson 2 we learned how to create variables and how to display those
variables in our Web Browser. Now that we can create variables, we’re going to
learn how to do something useful.

Monday, November 24, 2008

Lesson 1 : Hello World

Lesson 1 : Hello World

Objective
In the programming world, no matter what language you are learning, “Hello
World” is the first thing that you learn. This lesson will teach you how to create a
PHP file and how to use PHP to print the words “Hello World” on your screen.

Step 1
Open your Text Editing Program. On Windows, you can use the program named
“Notepad”. On a Mac, you can use the program named “TextEdit”.

Step 2
Now we’re going to type a few lines of code. Type the lines just as they appear in
the text below:


Step 3
Now that you have typed the text from the grey box into your window, you need
to save the file. At the top of your screen click File > Save As… and navigate to
the Projects Folder that you created on your desktop.

Step 4
You will need to type in a name for your PHP file. Name this file hello.php.

Step 5
Now that you have saved hello.php to your Projects Folder, it is time to upload it
to your server. Open your FTP Program and connect to your server.

Step 6
Once you are connected to your server, click on hello.php in your FTP Program
and drag hello.php to the main directory on your server. Allow a moment for the
file to transfer.

Step 7
If you have completed all of these tasks correctly, you should be ready to view
your file online. Open your Web Browser, and using your web domain, type in
www.YourWebDomain.com/hello.php. You should see the words “Hello World” in
your Web Browser.

Step 8
If you see the words “Hello World” in your Web Browser, you have successfully
completed Lesson 1. If something seems to have gone wrong, go back and give
it another try.


We just created the simplest of all PHP programs. Now we’ll go line by line to
help understand what we did.
Line 1

Line 2

echo “Hello World”;

This line of code uses the echo command. Whenever you want to display text on
the screen, you use the echo command. The text that you wish to print to the
screen must be enclosed between quotes. The line must end with a semicolon.


Line 3

?>
This will be the final line to every PHP file that you ever create. This tells the
server that the current section of PHP code has come to an end.


Exercise
Now that you can get your file to display “Hello World” in your Web Browser, try
altering the file and make it display a different message.

Conclusion
In Lesson 1 we learned how to output a simple line of text. While this may seem
like a silly and pointless exercise, we will use this as the foundation to build more
useful skills.