Previous | Home | Next |
The PHP-variable means of storing a value, such as text string " r4rtechsoft " or the integer value 8. A variable can be re used throughout your code over and over again.
In PHP can defined by a variable with the following form:
$variable_name = Value;
If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!
Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_rollnumber and $A_rollnumber are different variables in PHP's Programming.
Example:
Say that we wanted to store the values that we talked about in the above paragraph. How would we go about doing this? We would first want to make a variable name and then set that equal to the value we want. See our example below for the correct way to do this.
PHP Code: <?php $hello = "r4rtechsoft"; $a_rollnumber = 4; $anotherRollNumber = 8; ?>
PHP Variable Naming Conventions:
The PHP variables are must starting with a letter or underscore "_".The PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ . And Variables with more than one word should be separated with underscores. $my_variable with more than one word can also be distinguished with capitalization. $myVariable.
Example:
If you're ok with the idea of variables, then you can move on. If not, think of them like this. Suppose you want to catalogue your clothing collection. You enlist two people to help you, a man and a woman. These two people are going to be your storage areas. They are going to hold things for you, while you tally up what you own. The man and the woman, then, are variables.
You count how many coats you have, and then give these to the man. You count how many shoes you have, and give these to the woman. Unfortunately, you have a bad memory. The question is, which one of your people (variables) holds the coats and which one holds the shoes? To help you remember, you can give your people names! You could call them something like this:
mr_coats mrs_shoes
But it's entirely up to you what names you give your people (variables). If you like, they could be called this:
man_coats woman_shoes
Or
HimCoats HerShoes
But because your memory is bad, it's best to give them names that help you remember what it is they are holding for you. (There are some things your people balk at being called. You can't begin their names with an underscore (_), or a number. But most other characters are fine.)
OK, so your people (variables) now have name. But it's no good just giving them a name. They are going to be doing some work for you, so you need to tell them what they will be doing. The man is going to be holding the coats. But we can specify how many coats he will be holding. If you have ten coats to give him, then you do the "telling" like this:
mr_coats = 10So, the variable name comes first, then an equals sign. After the equals sign, you tell your variable what it will be doing. Holding the number 10, in our case. (The equals sign, by the way, is not really an equals sign. It's called an assignment operator. But don't worry about it, at this stage. Just remember that you need the equals sign to store things in your variables.)
However, you're learning PHP, so there's something missing. Two things, actually. First, your people (variables) need a dollar sign at the beginning (people are like that). So it would be this:
$mr_coats = 10
If you miss the dollar sign out, then your people will refuse to work! But the other thing missing is something really picky and fussy - a semi-colon. Lines of code in PHP need a semi-colon at the end:
$mr_coats = 10;
If you get any parse errors when you try to run your code, the first thing to check is if you've missed the semi-colon off the end. It's very easy to do, and can be frustrating. The next thing to check is if you've missed out a dollar sign. But back to our people (variables).
So the man is holding ten coats. We can do the same thing with the other person (variable):
$mrs_shoes = 25;
So, $mrs_shoes is holding a value of 25. If we then wanted to add up how many items of clothes we have so far, we could set up a new variable (Note the dollar sign at the beginning of the new variable):
$total_clothes
We can then add up the coats and the shoes. You add up in PHP like this:
$total_clothes = $mr_coats + $mrs_shoes;
Remember, $mr_coats is holding a value of 10, and $mrs_shoes is holding a value of 25. If you use a plus sign, PHP thinks you want to add up. So it will work out the total for you. The answer will then get stored in our new variable, the one we've called $total_clothes. You can also add up like this:
$total_clothes = 10 + 25;
Again, PHP will see the plus sign and add the two together for you. Of course, you can add up more than two items:
$total_clothes = 10 + 25 + 7 + 38 + 1250;
But the idea is the same - PHP will see plus signs and then add things up. The answer is then stored in your variable name, the one to the left of the equals sign.
In the previous section, you saw how to put numbers into variables. But you can also put text into your variables. Suppose you want to know something about the coats you own. Are they Winter coats? Jackets? Summer coats? You decide to catalogue this, as well. You can put direct text into your variables. You do it in a similar way to storing numbers:
$coats1 = "Winter Coats";
Again, our variable name starts with a dollar sign ($). We've then given it the name coats1. The equals sign follows the variable name. After the equals sign, however, we have direct text - Winter Coats. But notice the double quotation marks around our text. If you don't surround your direct text with quotation marks, then you'll get errors. You can, however, use single quotes instead of double quotes. So you can do this:
$coats1 = 'Winter Coats';
But you can't do this:
$coats1 = 'Winter Coats";
In the above line, we've started with a single quote and ended with a double quote. This will get you an error.
We can store other text in the same way:
$coats2 = "Jackets"; $coats3 = "Summer Coats";
The direct text will then get stored in the variable to the left of the equals sign.So, to recap, variables are storage areas. You use these storage areas to manipulate things like text and numbers. You'll be using variables a lot, and on the next few pages you'll see how they work in practice.
There are few things you need to note when working with PHP variables:
The variable name should only start with underscore "_" or a letter. and it's can only contain letters, numbers or underscore. In another words, you can't use other funky characters like <^# in your variables names.
Previous | Home | Next |