What Are Variables?
PHP Variables
We can all probably remember variables from algebra class during our high school years, remember expressions like x + 2 = 10 or something fun like 5x(-3x3)2 ? Well, maybe some of us can, don’t worry were not going to be solving any equations here. Anyways, besides this nostalgic moment, how do variables fit into the picture?
A variable is a location in a computer’s memory, identified by a symbolic name, that can hold different values of data at different times during the execution of a program. Variables can store and retrieve data that is essential to your scripts, things like the number of visitors to your website, the total amount due at checkout, the name of the person currently logined in, or the age of the user can all be stored in variables.
To help explain the concept of variables in more practical terms, we can visualize variables as rows of P.O. boxes located in a post office (computer memory) were each P.O. box (variable) is identified by a unique number (variable name). You can retrieve mail from your P.O. box by referring to its number, similarly you can retrieve data from a variable by referring to its name.
Creating and Assigning Variables
User created variable names or identifiers need to follow a certain set of rules to be valid. In PHP, all variable names must begin with the dollar sign ($), followed by either a letter or an underscore character and it cannot begin with a digit. Variable names are case-sensitive, so $age is not the same as $Age or$AGE. The assignment statement is used to assign or store data values into a variable using the assignment operator (=). The assignment operator is an equal sign, which in this case semantically means to store or “assign” a value to a variable. Note that a semicolon appears at the end of each assignment statement.
Lets look at a few examples:
<php /* the following are all valid names */ $age = 23; $_total = 12.99; $name = "John Smith"; $_232 = 53; $name = 225; /* the following are all invalid */ $2age = 21; $@total = 9.99; $-var5 = "John Smith"; ?>
Unlike other programming languages like C++ or Perl, you don’t have to explicitly declare or assign a particular data type to variables before using them. You can create variables on the fly and change their values throughout your scripts, PHP will dynamically change between data types depending on the value it contains. Consider the example above, we first assigned the string value "John Smith" to the $name variable and later changed the value to 225, which is an integer.
Expressions consisting of constants, operators, and other variables are acceptable values for variables. PHP will evaluate the expression and assign the result to be stored in the variable. A variable will keep the same value throughout the script until another assignment statement assigns it a different value. Here are a few examples:
<php $sum = 20 + 10; echo "<p>$sum</p>"; $sum = 15; /* $sum is assigned a new value of 15 */ $avg = 120 / 6; $total = $sum + $avg; echo "<p>$sum</p>"; echo "<p>$avg</p>"; echo "<p>the total is : $total</p>"; ?>
This is the output from the example above:
30
15
20
the total is : 35
Variables can store different values at varying times and because of this dynamic functionality, it allows programmers to easily manipulate and transfer data throughout their applications. Next we’ll learn about constants and explore how they differ from variables.
