In computer programming, a variable is a name for some data we are processing. Variable names are typically short, convenient, easy to remember, easy to type.
You can store data in a variable, and then use the variable with other PHP commands to retrieve the data, combine two pieces of data together, compare variables, and perform a variety of other tasks.
The variable name is the key to a value. The value is what is stored inside the variable name. A value can be a number, a name, an entire dictionary of text, a program that carry out further actions, and other kinds of data.
In PHP, use the dollar sign symbol to declare a variable and assign a value, like this:
$test = “hello world”;
echo $test; // this will echo “hello world”
System variables begin with a dollar sign followed by an underscore ($_) — you may use these variables, but you may not change them.
Variables are commonly use to making programs easier to type, read, and remember, by allowing you to give short names of your own choosing to more complicated data.
For example, say that you need this line of code to appear several times in your page:
You might prefer to do this:
$fname = $_GET[‘firstname’];
// … some other intervening code …
In the above example, we define a short variable name — $fname — and assign it the value of whatever is $_GET[‘firstname’]. This is useful if you find $fname easier to remember and type than $_GET[‘firstname’].
Or perhaps you are working with $_GET[‘fname’], and you find $firstname more convenient. Use code like this:
$firstname = $_GET[‘fname’];