Variables in PHP used for the same reason like in any other programming scripting language, i.e., to store dataIf we want to store any type of data for later use or to do some operation on it then we need to store it inside a variable. We can say that variables are like containers storing a particular type of data.
A variable can store different types of values like it can store
integer value, float value, a string, boolean value, etc.In other languages, the programmer needs to specify first, what kind of data the variable he is about to declare, will store, i.e., whether it is an integer value, a string, a float value, etc.. these are called Strongly Typed Languages. The opposite of these languages is a Loosely Typed Language, which are not that much strict and allows the programmer to avoid declaring the data type of the variable's value, they figure out it of their own.
'$' sign. We will have a look at it in the examples, then you will have a clear idea about it, let's first got through some rules to define a variable in PHP.
Every language has some rules which a programmer has to follow in order to code in a well defined manner. Some rules regarding defining PHP Variables are:
$) sign followed by the variable identifier. An identifier is any name given to the variable.
'a-z', 'A-Z', '0-9' and '_'.
Let's quickly go through an example of how to create a PHP variable. We have already discussed it earlier that you need to use the '$' sign to declare the variable in PHP and not need to specify the data type of the value you are going to store in the variable.
In the example above, we have three variables
'$txt', $x and $y.
'$txt' has string value but
'$y' has integer value.
We have not specified any datatype for them but still there are no errors because of using
'echo()' function is used to print the values of the variables, notice that here also we don't need to
specify any data type.
$marks;- Not allowed
Let's go through another example of adding two PHP variables' values. Remember that you cannot add a string value with an integer. We will use two integer values stored in two different variables and by using a third variable we will add their values:
In the example above, as you can see the
'$z' variable is used to add the values of
'$y' by using the
'+' addition operator.
The scope of PHP variable can be defined as to which extent we can use it inside the program. Whether we can only use the variable inside a single function or we can use it with multiple functions? So the scope of the variable will be the portion of code which is allowed to use that PHP variable's value, if your code cannot use a Variable's value then it is out of scope. It is an easy concept, we will look at different types of scopes with proper examples which will clear out things for you.
PHP variables which are not declared inside any function and can be used by any function are called Global variables.
The advantage of using these variables is that any function can use it's value to do any kind of operation.
It helps in saving time and memory by not creating variables again and again for a same value.
But to use these variables inside a function we have to use
Let's see an example:
In the example above, when we tried to access the global variable
'$x' inside the
'myTest()' function, it showed an error stating 'undefined variable: x', because it is a global variable and we cannot use it directly within any function, we have to use the
Whereas when we tried to use
'$x' outside of any function then we can access it normally without
$GLOBALS array stores all the global variables created in a program.
The index of this array holds the name of the Global variable. So whenever any function needs to access a global variable it can use this array.
Let's see an exmaple:
In the example above, we access the global variables
'$y' inside the
'myTest()' function by using the
'$GLOBALS' arrray and then added both values to
Although, we have not printed the value inside the function but you can do it by using
echo($GLOBALS['y']);. Try it.
The variables which are declared inside a function and can only be used within that particular function and not outside of it, are called Local Variables. If we declare any other variable with the same name outside the function as a variable inside the function then it will work as a whole different variable.
Let's see an example to clear out things:
In the example above, we have tried to access
'$x', which is a local variable to
'myTest()' function, outside the function.
It will show an error
undefined variable: x because we cannot use
'$x' outside the function unless there is another variable named
'$x' exist outside the
PHP always deletes the variables of a function from the memory after the successful execution of the fucntion. But what if we need any variable's value even after the execution of a function, like we may need to increment or decrement the value multiple times. So, what we can do to stop PHP from deleting variable from the memory?, we use the static variables.
Static variables in PHP are declared using the keyword
'static'. After declaring any PHP variable 'static', it will not get deleted from the memory till the program is getting executed.
Let's see an example of static keywords:
In the example above, we have declared
'$x' as static. It haven't got destroyed after each fucntion call so it gets incremented by
'1' each time it is called and we got an incremented value.
So, you can say that in each function call the variable retains the value of it's previous function calls.
But if we won't use the static keyword with
'$x' then you will get the output as
0 0 0 because after each execution of the function, the variable gets destroyed and it is initialized again the value
We have till now used the
'$' sign to create variables in PHP but there is another
'$$' sign which it used.
So, what's the difference between these two? Let's see:
We are familiar with the working of
'$', it creates the variable in PHP and stores a value in it but the
'$$' is known as a reference variable.
The '$$' variable is used to refer to the value of the '$' variable. It means that when we use the '$$' variable, we are indirectly referrring to a variable named as value stored in the '$' variable.
Let's see an example to clear out things:
In the example above, we have two variables
'$Hello'. As you can see
'$Hello' is named same as the value of
When we printed
'$var' then the value
"Hello" gets printed which is normal, but when we printed
'$$var', then the PHP interpreter looked for the value of
'$var', which is
'Hello' and then looked for a variable named as
'$Hello' and printed its value.
Create some programs by yourself and then you will get a clear idea of how to use these variables if things are still not clear to you.
It was all about PHP variables and we hope that we have made it easy for you to understand it easily. Stay with us to get more PHP tutorials, click on next.