Where you can learn to Be Your Own Geek!
If you've done any other programming in any other programming language before it's quite likely you would be familiar with the term "variable".If on the other hand you are anything like what I was when i was starting out to teach myself some programming, it didn't matter what prgramming language i was trying to learn, I would always find myself looking at a tutorial that told me to use a variable but didn't explain what the heck a variable was. This can make it very confusing for the beginner who hasn't had any previous programming experience in whatever language before.
So just what is a variable? The easiest way to think of a variable is to think of it as a container like a bag or box in which you can store information, data and stuff for use in your programs.
A common phrase you will hear in programming is "declaring a variable". This is simply a geeky way of saying "creating a variable" which usually involves naming the variable and defining the type of data it can store, (usually text or numerical data) before we can actually use the variable.
In php we don't need to worry too much about declaring a variable before using it as php automatically declares our variable as soon as we use it so all we need to do is decide what to name our variable.
rules for php variable names
In php(and most other programming languages), variables always start with a "$" sign followed by the name we give it.
With a few exceptions, you can name a variable almost anything you want to but they must start with either a letter(from a to z or A to Z ) or an underscore "_". It cannot start with a number.
The rest of your variable name can contain alpha-numeric characters and underscores(a to z, A to Z, 0 to 9,and _) but it cannot contain any other characters such as @,#,%,^ etc.....
Variable names cannot contain spaces either. If you want to use two words in your variable name, it is usually recommended to seperate the words either with an underscore ("_") or with capitalization eg oneTwo.
Below are some acceptable variable names:
In php, variable names are case sensative:
is not the same as:
sensible variable names
While you can name a variable pretty much anything you want to, it's always good practice to choose a name that makes sense within the rest of your script just to make it easier for yourself as a programmer or for some other poor confused coder who might be trying to understand your code years down the track in the future.
So if you had a sword, a staff and a mace that you wanted to put all in the same container, you might choose a variable name such as $weapon_box rather than $spell_book, save that for your fire shield, ice bolt and thunder ball.
Let's look at the first line of code after our opening php tag in that example:
Here we've named a variable "$middle" (because it's used near the middle section of our sentance) and we've stored the word "was" in it by using an equals sign ("=").
This is also known as assigning a value to variable. If the value is an array(we'll get to arrays later), a string, ie: a word, a sentance, chunk of text or anything that does not have a numerical value, we must enclose it in quotes. You can use single or double quotes, but make sure your openening and closing quotes match. Later on with much trickier code you may need to use a combination of both. For now just try to choose single or double and practice being consistant with whichever you choose where you can.
One more thing to note before we move on to the next lesson is the semi colon(";") at the end of each line of code in our happy_sad_what.php file. Each line of code is an instruction we are asking php to follow, $middle='was'; is instructing php to store the word "was" in our variable. The semi-colon tells php that it has come to the end of that particular instruction and so anything that comes after the semi-colon is a brand new instruction.
Before we move on to the next lesson, take a few moments to edit your happy_sad_what.php file, try changing the quotes, leave a closing quote off or close it with a double quote. Try leaving one of the semi-colons off. See what happens when you try to run your script and you will notice that php is very picky about the tiniest little things.
Click here to continue to php lesson 6 where we'll break down our happy_sad_what.php file a bit more and take a look at arrays.
Lesson added Monday 3rd August 2009
things to do list:
© 2008-2009 BYOGeek.com / nesperion.net