PHP For Beginners – Introduction to Variables
Apr9Written by:
2010/04/09 07:26 AM
Variables are an integral part of any programming language. PHP is no exception. While different programs have different rules with regards to variable use and declaration. They are all used for the same thing, that is storing data. Any type of data.
Image via Wikipedia
Image via Wikipedia
What is a variable?
Think of a variable as a little box in memory where you can store stuff. Like a shoe box. A shoe box might have one shoe or two shoes in it. But then it might have eggs in it, or it might have money in it. What ever data you can think of, your variable should be able to hold it.
Variables are used for storing values, like text strings, numbers or arrays. The beauty about variables is that you can use them over and over again without needing to know what they contain.
Remember algebra in school where a letter stood for some value. Well variables are just like that. x=5, y=10, z=x^y.
A good example is storing the value of Pi in a variable. Then using that simple variable in calculations. Because Pi is a difficult value to remember, putting it into a variable with an easy to remember name, makes it easier to program with.
For example, if you wanted to work out the area of a circle and you had to do it many times in your program. Using a variable is the sensible thing to do.
<?php
$pi = 3.14159265358979323846264338327950288419716939937510582097
4944592307816406286208998628034825342117067;
r = 5;
$AreaOfCircle = $pi*r*r;
?>
Fortunately PHP has a function to return pi to 11 decimals.
Naming rules for variables
Just like any programming language. PHP has some specific rules when naming variables. They are:
- All variables in PHP start with a $ sign symbol.
- A variable name must start with a letter or an underscore "_"
- A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
- A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
The correct way of declaring a variable in PHP would be:
$Variable_Name = value;
PHP is a loosely typed language
A strongly typed language like C# means that you have to specifically define the data type of the variable before you use it. Then you can only store that data type in that specific variable.
For example if you want to store an integer value you would have to declare a variable of type integer.
In PHP, a variable does not need to be declared before adding a value to it, nor do they need to be specifically typed.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In PHP, the variable is declared automatically when you use it.
Using Variables
So now that we now what a variable is, how do we use them? There are three basic things you can do with variables:
- Set them (give them one or more values);
- Re-set them if they were set before;
- Access them (read the value of a variable and then do something useful with it).
To assign a value to a variable, you use the assignment operator: the = symbol. This is used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables.
To set a variable:
<?php
$age = 42;
$name = "Robert";
?>
To re-set a variable:
<?php
$age = 24;
?>
Access and use the variable
<?php
$age = 42;
$name = "Robert";
echo "My name is ".$name." I am ".$age." years old";
?>
or
<?php
$age = 42;
$name = "Robert";
echo "My name is $name I am $age years old";
?>
Would print out:
My name is Robert I am 42 years old
Conclusion
There are so many uses for variables that it is beyond the scope of this post. Suffice to say that you need to familiarise yourself with variables and their use as you will undoubtedly use them everywhere in your PHP programming.
Next lesson we will be looking at more uses of variables as well as the different data types you can use with variables.
BTW, here is a question for you. What is the value of z?
Remember you can win a weeks worth of free ad space just for commenting. The best comments during the week will get a weeks free advertising space. Check out: Win free advertising for your blog
New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.
Get involved in our community. Help promote other bloggers. List your blog in our directory for bloggers. Blog Directory for Bloggers
blog comments powered by