Lesson #3 (Soon)
In just about any PHP script you do, you'll need to use variables. Variables can be thought of as containers of code, both dynamic and static. This is extremely useful for keeping your code shorter. For example, instead of typing out a string of code over and over, you can assign a variable and simply reference the variable when the code is needed.
Here is the script we'll be working with today. I'll gradually break it down and explain what everything means. If I've missed something or you have a question post below and I'll do my best to answer it.
Remember that it's always best if you type the code yourself instead of copy/pasting it. Trust me you'll learn much faster this way.
PHP Code:
<?php
// This is the variable
$name = "Thomas";
//This is echoing the variable
echo "Welcome, $name<BR>";
//This is how to echo a variable with single quotations
echo 'Welcome, '.$name.' how are you?';
?>
In today's script, we're going to be declaring a variable ($name) and assigning it data (Thomas). After we've declared the variable we're going to be echoing the variables's data in both double and single quotes.
What's the difference between double and single quotes?
Double and single quotes differ in that a double quote will render out any PHP code, or anything that could be mistaken as PHP code within the double quotes. This can be troublesome in that it may cause errors in the final code. So in order to fix this we use single quotations. Anything that is, or may be mistaken as PHP code will not be rendered unless you close the quote and append a full stop on both the front and back of the code, then re-opening the single quotes to finish out your code. If this has confused you look at the code above. You'll notice it on line 7.How do you declare a variable?
Declaring a variable is extremely simple. You declare a variable by doing the following.In this code $name is the variable, and "Thomas" is the data in which the variable will be passing. Remember to enclose your variable's data in quotes, you may use both single and double quotes. This will use the same rules as explained previously.PHP Code:
<?php
$name = "Thomas";
?>
To help demonstrate how a variable works, we'll be echoing (see lesson #1) out the variable's data. We can do this by typing the following.
Snippet two will cause the document to be rendered as "Welcome, Thomas". The <BR> at the end of the line is HTML code and will cause the following code to be rendered on a new line.PHP Code:
<?php
//Snippet 1
$name = "Thomas";
//Snippet 2
echo "Welcome, $name<BR>";
//Snippet 3
echo 'Welcome, '.$name.' how are you?';
?>
Snippet three is an example of how you can echo a variable with single quotes. Reference my earlier explanation for details.
That should be the basics for how to use variables, and how they work. If you have any questions please feel free to post below. I'll do my best to anser them quickly.
As always, you may see the finished code by visiting the following URL.
http://toastur.com/projects/tutorials/2.php
Results 1 to 3 of 3
Thread: [PHP] Lesson #2 - Variables
- 07 Feb. 2012 08:56pm #1
[PHP] Lesson #2 - Variables
Last edited by Elliot; 07 Feb. 2012 at 09:01pm.
// Signature
- 08 Feb. 2012 03:01pm #2
- 14 Feb. 2012 06:55pm #3