Tutorial 2 – PHP Variables
In the earlier PHP tutorial we saw an introduction to what PHP is and how it is used in web programming. Click here to read that Post
In this tutorial on PHP Variables we will discuss the following:
- What is a variable?
- Variables in PHP
- PHP is a loosely typed language
- Variable naming rules in PHP
- Use of the double dollar ($$) variables
What is a variable?
A variable is the name given to a memory allocation to store or retrieve information. A variable can be used to store values like strings (text), numbers, array or objects. Once a variable is set it can be used anywhere in your script.
Variables in PHP
In PHP a variable is declared using the dollar ($) symbol. The dollar symbol instructions the PHP language that you are interested in creating a variable. Look at the example below:
$a = “SUNIL”;
In the above example you will see that I have defined a variable by the name $a. $a is nothing but a name given to the memory allocation where ‘SUNIL’ is stored. Before a variable is created a space in the memory is allocated to that variable and then that space is associated with the variable name. This is called memory allocation.
Look at the diagram below to understand the internals of a memory and how it is assigned:
In the above example the series of blocks that you see is memory. The numbers 100, 101, 102, etc are numbers assigned to each memory block which is 1 byte each. As per the example above, when you assign the value “SUNIL” to $a this is how the internals look like.
PHP is a loosely typed language
Unlike languages like C, Java & C#; PHP is a loosely typed language. This means that you don’t have to declare the variable before using it and you don’t need to specify the data type of the variable.
Data type of the variable means that type of data that can be stored in the variable. In languages like ‘C’ you cannot declare a variable before specifying its data type. The data type could either be character, integer or floating point numbers. This means that if a variable is defined as a number you cannot assign a string to it.
You can see from the example above that I did not have to define the type of a variable. I just had to create a variable and assign memory to it like $a = “SUNIL”;
This means that in PHP, variables are declared automatically as you use it.
Variable naming rules in PHP
- A variable name must start with a letter or an underscore “_”
- A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
- A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
Use of double dollar ($$) sign in PHP
Double dollar is a powerful way to programmatically create variables and assign values them.
$a = “amount”;
echo $amount; //echo’s 1000 on screen
In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.
So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.
In the next post I will be talking more on PHP Strings. Subscribe below to receive updates on the next tutorial as it gets published.