Variables and the Echo Statement (PHP)
by Varun ThirayanAPDZ.com
Thursday, 9th February 2006
Setting Variables
Before setting a variable it might help to know what one actually is. A variable is basically a piece of memory which has a name and holds a 'value'. Open up your favourite HTML/PHP text editor, and type the text below.
<?php
$partA = "Hello";
$partB = "World!";
?>
$partA = "Hello";
$partB = "World!";
?>
Above we have written a very small PHP code, at the moment nothing will appear in the browser. The first line and last line are the basic PHP start/end tags. The thing that we want to be looking at is the two variables $partA and $partB. The two values are set inside the speech marks ("value"). This is the way textual values are allways set.
When calling or setting variables, they must start with a dollar tag ($) and have a name with no spaces. Just like (nearly) all other lines of PHP code, the line must end with a ';'. Take a look at the two codes below, see if you can spot the differences and identify which should work.
<?php
$partA = "Hello";
$partB = "World!";
?>
$partA = "Hello";
$partB = "World!";
?>
//-------------------------
<?php
$part A = Hello";
$partB = "World!"
?>
$part A = Hello";
$partB = "World!"
?>
There are three very obivious and also very important differences in the above lines which will have a major effect on the outcome. The php highlighting has allready picked it out...
Variable Name: There cannot and must not be a space in the name of a variable. Therefore the top code is correct.
Speech Marks "...": If the value of the variable is going to be textual, (ie. Hello) it MUST be surrounded by speech marks. Again making the code above the line correct.
The ends of the lines: All lines must end with a ;. As you can see the variable $partB does not end with a ;. Therefore once again, the code above is the only one that works.
When working with numbers only however the speech marks are not necessary. Therefore numbers can be set as below.
<?php
$partA = 3;
$partB = 23;
$partC = 5.1;
?>
$partA = 3;
$partB = 23;
$partC = 5.1;
?>
There will be more about using numbers as values in a later tutorial. Just remember that only numbers can be written this way (no text can be included).
Finally another, but equally important thing to remember is that variable names are case sensitive. Ie. $partB is very different to $Partb!! They are two completly different variables.
The Echo Statement
Now that we have set these variables, we need to display them on the screen so that they can be viewed by the user. The 'echo' statement is probably the easiest way to do this. Refering back to the code we used earlier, we could try something like this:
<?php
$partA = "Hello";
$partB = "World!";
echo $partA . $partB;
?>
$partA = "Hello";
$partB = "World!";
echo $partA . $partB;
?>
Will Display:
HelloWorld!
Now have a look at what this code will display. Notice that the period (.) operator is used to 'join' two items together and the last thing to notice is the fact that the line once again ends with a ;. As you can see, what we want to be printed on the page is simply placed after the word echo.
One small downside of the output of this code could be the fact that there is no space!... So... What do we do? There are a couple of options available.
Firstly, you could set another variable called $space and give it the value " ". You could then use another period operator and place it inbetween partA and partB. Try this one for yourself
Another option, and the one that I am going to use, is placing pure text within an echo statement. The best way to explain this is with an example.
<?php
$partA = "Hello";
$partB = "World!";
echo $partA . " " . $partB;
?>
$partA = "Hello";
$partB = "World!";
echo $partA . " " . $partB;
?>
Will Display:
Hello World!
Using another period operator, we have been able to place a space inbetween the two variables. You have now joined the variable $partA with a space (pure text inside the speech marks) and joined it with the variable $partB.
Try out all the options above and change them. Try adding more variables and more echo statements, try making errors on purpose to see what the outcome is. Take out the variables completly and just try echo-ing pure text. That's the best way to practice.
Options:
Printer Friendly
