PHP Tutorials: All About Variables

Hey Guys,

In general terms, a variable is used to store a value or data i.e. number, string, array, etc. You might have read this statement everywhere. But what actually a variable is, and how does it work?

What is a variable?

Many programmers fail due to lack of knowledge of variables. A variable is as simple as anything can be in programming but it can be as complex as you make it. First, let us see what a variable is.

In technical terms, Variable is a name given to a location in Physical Memory. Physical Memory? Yes, physical memory and it’s your PC’s RAM. Any program running on your PC actually takes space on your RAM/Physical Memory, which makes your program run fast and efficiently. Now, when you assign a value to the variable it gets stored in a particular memory location to which the variable is referring to. A variable name is similar to your name in real life. If somebody has to assign you any task, they will call you by your name and will assign.

Whenever a variable gets reassignment, the value in the memory location will change at the same time. Let us take an example.

<?php
     // A memory location will be assiged to $a and 
     // 10 will be stored in that location
     $a = 10;
     echo $a; //OUTPUT 10
     // Here the value on the memory location will 
     // be replace with 15.
     $a = 15; //OUTPUT 15
     
     //A new memory location will be created and assigned
     //name $b
     $b = 25;
     echo $b; //OUTPUT 25
?>

I hope the above example given you an idea about a variable. Now, there are a certain set of rules which a good programmer should follow while naming a variable.

  • Every variable in PHP starts with $
  • It should contain alphabet, number and only _ (underscore) as a special character.
  • It can not start with a number
  • It can be started with _ (underscore)
  • It can not contain spaces
  • The name of the variable should be logical so you can understand in the future why you had declared it. For example, if you have to declare a variable to store Username then name it as $username. If you have to declare it from Previous year marks then $previous_year_marks.

What is the scope of a variable?

Everything in this world has a scope. Beyond that and before that the thing has not life. The same rule applies to a variable. Before you declare it or in another file, a normal variable does not have any value and it will display a warning “undefined variable”. Let us take an example.

<?php 
     // Code in file1.php
     echo $a; //Undefined error
     $a = 10;
     echo $a; //OUTPUT 10
?>
<?php 
     // Code in file2.php
     echo $a; //Undefined error
?>

In the above 2 examples, you will get an idea about the scope of a variable. Later we will see how we can define a scope increase the accessibility of a variable.

<?php
function scopeCheck(){
	$name = "John Doe";
	echo $name;
}
scopeCheck(); //John Doe
echo $name; //undefined variable
?>

In the above example, you will see, the $name has a value inside the function but not outside the function.

<?php
$name = "John Doe";
function scopeCheck(){
	echo $name;
}
scopeCheck(); //undefined variable
echo $name; //John Doe

?>

Although the variable $name in the above example has a value but it has no scope inside the function. The scope of a variable declared inside the function has no scope outside of its block and the variable declared outside has no scope inside the function.

Global Variable

4 thoughts on “PHP Tutorials: All About Variables

Leave a Reply

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