PHP Variables

Published on April 6th, 2013 by | Category: Programming

I have already talked about the PHP basics and requirements (find the previous articles here), and basic HTML and PHP syntax. Now we are getting closer to professional PHP programming. In this article, I am talking about the variables that is one of the most important topics in professional programming.

So far, you have learned how to send HTML to the web browser through PHP. The question is while we can do this using a simple HTML page, why should we use PHP to do that?

We are learning PHP programming because we want to create dynamic web pages, and this can not be done using the simple HTML files. To create dynamic websites and pages, we need a programming language like PHP. And to use PHP to create dynamic pages, we have to use variables. I will tell you what variables are. However, first let me tell you that we are not able to define and use variables in HTML. Therefore, HTML is not known as a programming language, because it doesn’t support variables.

What Are Variables?

I do not want to give you the official definition of variable, because it can make you confused. I want to help you understand what variables are through giving a simple example. Let’s say you have a grocery store and you sell fruits. Do you mix all kinds of fruits with each other in one container, or you prefer to have one separate container for each kind of fruit? I am sure you prefer to have a separate container for each kind of fruit.

Now, let’s get back to programming. Like a grocery store that you have different kinds of fruits, in programming you have different kinds of data, we need a separate container for each. We have numbers, names, texts, and… and each of them have to be kept in a separate container. Variables are the data containers. We use them to keep the data in them for a limited time. Additionally, when we want to change a data, we put it in the variable, change it, and then use it.

Variables Are Data Containers.

The data you put in the variable is the value of that variable.

Variables exist as long as the script is working. When the script reaches to the PHP closing tag, the variables that had been used in that script will be removed from the server memory until next time that the script is used again.

PHP Variables Syntax

How to Create and Define Variables in PHP?

Any programming language have its own rules for defining and creating the variables. In PHP you have to follow the below rules in creating and using the variables:

1. All the variables have to be started by a dollar sign ($).

2. After the $ sign, you have to use a letter or an underscore (_). You can not enter a number after the $ sign, but you can use the combination of letters, numbers or underscore after that.

3. You can not use space in the variable name. If you like to use several words in the variables names, you can simply separate them from each using underscore. You can also have each word start with an uppercase. This method of using the words in a combined word is called camel-hump or camel-case: ClientFirstName

4. You can not have two variables with the same name. Each variable has to have a different name.

5. Variable names are CaSe SeNsItIvE. However, do not use this as a way to have two different variables. For example, $Max and $max will be known by the system as two different variables, but if you like to have two different variables, do not name them like that. You can name them like this: $max_1 and $max_2

It will prevent you from getting confused later.

These are some examples of valid variable names we can define and use in our scripts:





As it was explained, variable names are case sensitive. Therefore, the system considers $Name and $name as two different variables. This is very important, and if you do not be careful about it, you will have bugs and errors that will be a little hard to locate.

And here is some examples of invalid variable names:

$First Name (it should not have a space).

$_First.Name (it should only have letters, underscore and numbers. Having period in the variables name is not allowed).

Family_Name (all the variable names have to be started with $ sign in PHP).

$1Family_Name (You can not have a number after the $ sign).

I prefer to choose descriptive names for variables. It makes it easier to remember why a variable is created. Also, you can always take the advantage of commenting to make it clear why each variable is created and what it is supposed to do:

An Exceptional Feature in PHP:

In many of the programming languages, you HAVE TO define each variable before using it. However, PHP is an exception. In PHP you do not have to define the variables before using them. So that, you can use variables in your codes even when they are not already defined. However, I do not recommend you to do this at all. It will make confusion both while coding and creating the script, and when referring to the codes to debug, modify or update in future.

Predefined Variables:

Some of the variables are predefined. It means they already exist and you do not have to create and define them. The system behaves as if these variables are already defined and their values are already determined. We will talk about several predefined variables in the next articles. However, here we will discuss about one of them to make you understand the basics better.

$_SERVER is a predefined variable. The system stores a lot of information about the server or the PHP-enabled computer in this variable. You can always refer to this variable and see its content, to have information about the server, or whenever you want to use this information somewhere in your script.

To see what is inside the $_SERVER variable (container), you can use the print_r() function—I hope you have already read my PHP Syntax article, so that you understand the print_r() function easier. Let’s use the print_r() function to print the $_SERVER value. To print the value of a variable using the print_r() function, all you have to do is that you place the variable name in the parenthesis: print_r(variable_name);

You should see such a thing on the page (click on the below image to see it in full size):

$_SERVER content

It is a good idea to print the value of variable when you have problems with your script. This can be known as a debugging method while developing your script.

In the next article, I will talk about the types of variables.

Leave a Reply

Your email address will not be published. Required fields are marked *