PHP 101: Understanding Variables
This is written for beginners. There may be aspects of the post which aren't technically true if you really know the ins and outs of PHP, but they're essentially true, and are a good starting point for people just starting to learn PHP.
Every programming language has what are called variables. You give them names and they hold values that you can use as you write your code. For example, let's say you need a variable to hold the name of the person visiting your web page because you want to use it in several places on the page and don't want to retrieve it several times. For now we'll just assign a static value (defeating the purpose of a variable), but we would do that by writing something like this...
$name = "John";
So we have a variable named $name, and the statement we just wrote reads "$name equals John". Actually it's better if you say "$name gets John" because we're assigning the value "John" to $name. $name doesn't actually equal (hold the value of) John until the statement has been executed. That will make more sense in a bit...
The other thing to notice is that we ended our PHP statement with a semicolon (;) - all PHP statments end in semicolons.
And lastly, variables start with dollar signs ($).
Now, stepping it up a bit... Let's say we want to assign a random number to some variable - that would look like this...
$n = rand(1,10);
This time we have a variable named $n which hold a random number between 1 and 10. "rand" is what is called a function. There are many functions in PHP that return values. That one returns a random value between the first and second number you specify. Notice that the function was followed by what are called "arguements" that are in parentheses. Even if a function takes no arguments it will have empty parentheses following it - it's part of being a function.
In the two examples you've seen that variables can hold textual strings like "John" as well as numbers. While the "type" of a variable can be important, it usually isn't. So for a beginner, just know that the variable has a type ("string" or "numeric"), but that you can use one type of variable in a situation that may call for another type of variable. For example...
<?php echo $n; ?>
You may not know what all that means, but what it's doing is echoing a value and returning that value into your document. Since documents are textual, it's returning a textual value, yet $n is a numeric variable. You don't need to do anything to get it to work properly - PHP will "cast" (change) the variable into the type that's needed at that moment.
There are times when you may see PHP's casting of variable types causing problems. For example if you sort some numbers and see something like this 1, 10, 2, 3, 4, 5, 6, 7, 8, 9 instead of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 then what that means is it treated a numeric variable as a string variable and since 10 started with 1 it put it with the other values that had one - much like you'd order "James", "John" and "Juan". Fixing this sort of problem isn't really a task for the total beginner - so for now just be aware that it can happen.
So to wrap things up...
- PHP variables start with $
- You assign values to variables using an equals sign (=)
- Variables can be textual or numeric
- PHP will change numeric variables into textual values as needed
- PHP statements always end with a semicolon (;)
Tags: CityTech ADV 4850
Categories: PHP
September 12th, 2008 at 5:09 pm
Keep it up: I’m just getting into php and your explanation makes sense!
September 12th, 2008 at 7:41 pm
@Owen – Glad it was useful!