PHP Variables Primer

This article continues from PHP 101-Learn the Very Basics of PHP. What in the world is a variable?! Think algebra. The wacky x + 5 = 15 stuff. In PHP it would be something like echo $x + 5;. "$x" would be the variable. If "$x" is nothing then PHP would echo 5. If "$x" were ten then PHP would echo 15. A variable is just a named chunk of memory for you to store information in. Assigning a variable is fairly simple. You could easily use something like var $x=5; or even simply $x=5; would work. If you want to assign a non number to a variable then you need to put it in quotes as a string like we mentioned above. An example would be something like echo "My dog is strange!";.

Also note that variables begin with a "$" sign. If a variable doesn't have a "$" then PHP will think it is a constant. We won't cover constants in this tutorial.

Let's consolidate some of the stuff we've figured out about PHP. The example below is a brief flex of our PHP understanding. There are plenty of things that just aren't very good practice in it but it does demonstrate some of what you can get away with in your PHP code.

This is html<br />
<?php
echo "This is PHP";echo "This will work too";
$x=5;
var $y=5;
echo $x+$y;
echo "$x+$y<br />";
echo '$x+$y<br />';
echo "X is $x<br />";
echo "Y is $y<br />";
?>

If you run the above code you should get the following output...

This is html
This is PHPThis will work too105+5
$x+$y
X is 5
Y is 5

Confusing? In the line echo "This is PHP";echo "This will work too"; notice how both echo statements are technically on the same line? This is possible because the afforementioned ";" tells PHP that they are seperate statements. Lines two and three simply demonstrate two different ways to assign variables in PHP. Line four adds the variables together. Notice there isn't an HTML line break tag at the end of any of these lines. That's why the output looks terrible

Line 5 is in quotation marks. Remember that this tells PHP that everything within is a string (part of the same group of letters, numbers and other characters). Previously I mentioned that PHP handled strings differently depending on whether they were in quotation marks or apostrophies. When in quotation marks PHP actually parses the string looking for variables. In this case it finds "$x" and "$y" and replaces them with there values which in this case would be "5".

The next line is the same thing but with apostrophies instead of quotation marks. In this example PHP does not parse the string for variables. It leaves the "$x" and "$y" as they are. In essence, when you encase a string in apostrophies PHP takes you literally. This actually saves PHP some work and increases performance because PHP doesn't have to parse the string. If you know a string doesn't need parsed then use apostrophies. In large scripts it may make a notable difference. If it's too confusing to start with then just use quotation marks if you want. No harm done while you are learning.

In case you are curious about the "+" in lines five and six, it is actually in your quotation marks and is therefore part of a string and not a mathematical operator. PHP replaces "$x" and "$y" with there respective values but the "+" stays as it is. The obvious "5+5" will remain as "5+5".

The final two lines again demonstrate how PHP parses strings in quotation marks and replaces variables with their values. We will learn a bit more about variables later on. For now I think we will go into some control structures. Sounds pretty scary huh? Your family might look at you a little odd if you start babeling insanely about control structures, strings and parsing at the dinner table. "Structure" is a good word to use in front of a boss at work every once in awhile though.

If you're curious to know how one would go about combining strings and variables and more string in an echo statement you'll find your answer is a simple period. A little vague I know so let's demonstrate...

<?php
echo $var1." hello ".$var2." $var3";
?>

If $var1="grand", $var2="morning" and $var3="dog" then the above code will output...

grand hello morning dog

Continue on to PHP Basic Loops and Conditional Structures

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
*