PHP Variables

A variable is simply a storage area. You put things to your storage areas (variables) so you can utilize and control them in your own programmes. Things you will want to store are text and numbers.

If you are okay with the notion of factors, then you can move on. If not, think of them like this. Suppose you want to catalog your clothing collection. You enlist two individuals that will assist you, a man and a girl. These two folks are going to function as storage areas. They are going to hold things for you, while you tally up what you own. The man and the girl, then, are variables.

You count how many coats you've got, then give these to the guy. You count the amount of shoes you have, and provide these to the woman. Unfortunately, you have a poor memory. The question is, which one of your people (factors ) retains the coats and which one holds the shoes? To help you remember, you may present your folks names! You could call them something like this:

mr_coats

mrs_shoes

But it's entirely up to you what titles you give your people (variables). If You Prefer, they could be called this:

man_coats

woman_shoes

Or

HimCoats

HerShoes

But since your memory is poor, it is ideal to give them titles that help you remember what it is they are holding for you. (There are a few things your people balk at being predicted. But most other characters really are fine.)

OK, so that your people (factors ) now have name. But it's no good simply giving them a name. They will do some work for you, so you have to inform them exactly what they'll do. The guy will be holding the coats. But we can define how many coats that he is going to be holding. If you have ten coats to provide him, then you really do the"telling" such as this:

mr_coats = 10

Following the equals sign, you inform your factor what it will do. Holding the number 10, in our case. (The equals sign, by the way, isn't actually an equals sign. It's known as an assignment operator. But do not worry about it, at this stage. Just remember that you need the input signal to store items on your variables.)

But you're learning PHP, so there is something missing. Two things, actually. First, your people (variables) need a dollar sign in the start (people are like that). So it'd be this:

$mr_coats = 10

If you miss the dollar sign out, then your visitors will refuse to work! But the other thing missing is something quite picky and fussy - a semi-colon. Lines of code in PHP require a semi-colon at the conclusion:

If you receive any parse errors when you attempt to run your code, the first thing to check is when you've overlooked the semi-colon off the finish. It's quite simple to accomplish, and can be frustrating. The next point to check is if you have missed out a dollar sign. However back to our individuals (variables).

Hence that the guy is holding ten coatings. We could do the same thing with another individual (factor ):

If we then wanted to include how many items of clothes we have so far, we can set up a brand new variable (Note the dollar sign at the begining of the new variable):

$total_clothes

We could then add up the coats and the sneakers. You put in 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 wish to add up. So it is going to work out the total for you. The solution will then get kept within our new variable, the one we have known as $total_clothes. You can also add up like this:

$total_clothes = 10 + 35;

gain, PHP will see the plus sign and insert the two together to you. Obviously, you can add up more than two things:

But the concept is the same - PHP will see and hints and add up things. The solution is then stored on your variable name, the one to the left of the equals sign.

In the next part, we will have a peek at how to put text to factors.