Explaining Variables In PHP

There are multiple types of variables to use in PHP. This tutorial will go through the different kinds of variables, as well as explain how they work. Some variables will have a follow up tutorial to explain them even further.

Numerical

Like any programming language you can define a numerical variable. Unlike other programming languages you do not have to declare the type of the variable. Thus you can have:

$var = 12;
and then you can have
$var = 12.5;
and it will automatically change.

The difference between the two variables is one is considered to be an int (short for integer) and one is a double. Integers do not have decimal values, while doubles do.

Strings

Strings can be anything. Numbers, letters, words, sentences. The only requirement is that when you define a string you add quotes to each end. Thus you have the following:

$var = "This is a string";
$var "32138**asjkas";
$var "abc123";
All of the above were examples of strings.

Arrays

Arrays are probably one of the most powerful types of variables, and one of the hardest to understand. An array can store multiple values. So say you had a customer with information of Name, Address, State, and Phone. You could have the following:

$customer = new array(); $customer[name] = "John Doe"; $customer[address] = "123 Main St."; $customer[state] = "Calafornia"; $customer[phone] = "5555551212";
and then the variable $customer would contain all of your information.

Constants

Constants are just that – constant. Also meaning that they don’t change. Unlike all of the other variables above, you cannot re-define a variable with the same name or change the value of them. To define a constant, you use the following:

define (“VALUE”, $var);
where VALUE is the value of the constant and $var is the name of the constant. These would be useful if you had to work with external files, database information, or anything else you may need to define one for.