Tuesday, November 25, 2008

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.

No comments: