Variables and Identifiers Explained

Although variable have been cover in previous articles they have not been officially covered. It is difficult to cover variables without the topics of identifier and datatypes as well. The first item we are going to tackle is the variable.

Variables

If you followed the article Set up your own local WampServer Installation , and if you haven’t -you should, you can fire up your WAMP server and your editor and test these examples on your local environment.

A variable is an item that can store different values at different points in time. In other words, it is a symbol for the data they represent. Let me show you what I mean. Take a look at the following code:

$price would be the variable and the integer 25 is the data. The “=” sign is the assignment operator and does not compare equality. This trips up some new developers because it is not initially intuitive but make sure you understand the difference between the assignment operator (“=”) and the equality operator (“==”). Operators will be covered more thoroughly in another article. Suffice it to say in this instance, $price is being “assigned” the value of 25. After this code is executed $price can now be thought of as 25.

You can demonstrate that by using the echo statement.

This will print “25” to the screen.  Printing to the screen was covered in another article called “Printing to the Screen with PHP.” The data that a variable stores can change. We can demonstrate this using the same script.

Echoing $price will now print 32 to the screen.  See the actual code below:

Viewing this page in a browser shows:

The price is 25
The new price is 32

NOTE*: The “.” that is used in the code block is the concatenation operator (yes another operator) that will be covered more thoroughly in an upcoming article. For now just know that it concatenates, or joins two string together. That is all it is doing here.

The variable name that is used is a user defined name given to a variable and these are called identifiers. That will be covered next but before leaving variables I think it is important to talk about what you name your variables. It is important that variable names have meaning. In our example we use $price. That could have been just as easily been named $p, however, that is not vary descriptive and would be hard to follow on a page with many lines of code. Keep variable names short but descriptive. Also, variable MUST begin with a $.

It is important to also note that variables do not have to be declared before they are used. This is not recommended and is not typical of most programming languages. A good practice is to declare the variable first with a comment before using it as shown in the updated code below:

Identifier

Identifiers are the names given to variables, functions and classes. Function and classes will be covered in an upcoming article. There are some rules when it comes to defining valid identifiers:

  • They can be of any length and can contain letters, numbers, and underscores.
  • They cannot begin with a digit. (They must begin with a letter or underscore)
  • Both functions and variables can have the same name. This should be avoided at all cost to avoid confusion.
  • Identifiers are case sensitive. $price is not the same variable as $Price. It is important to note functions are not case sensitive.

If you have any questions please leave a comment below.

Related Posts