Published: 1st Jan 2017 | Words: 1,869 | Reading Time: 10 minutes


Understanding PHP variables

Variables are probably one of the most simple but fundamental concepts within PHP and other languages such as JavaScript. Why are variables so important? Well a simple explanation is a variable can contain a certain value and can be called over and over as needed. In your PHP scripts you will probably have different values that are used multiple times over. Now instead of writing out these values multiple times throughout a script, you can store the value within a variable early on and then just call the variable. The benefit of this is that if you need to change the variable value, you change the initial value when you are setting the variable, and every call to the variable will be updated with the new value. Variables are declared within PHP scripts by using a dollar symbol followed by the name of the variable.

$myVariable = "Some value";

Allowing you to declare a lot of variables early on that may be used multiple times across scripts gives you an easier way of keeping track of variables and values instead of having to look through a lot of coding. This approach can also help with many other issues such as security and help in situations where there may be other programmers working on the PHP scripts. By declaring variables early on you create a blueprint of sorts which can guide other developers as to what the program does and how it functions.

echo "My name is Andy. People call me Andy";

My name is Andy. People call me Andy // this is printed to the screen

$name = "Andy";

echo "My name is ".$name.". People call me ".$name  

My name is Andy. People call me Andy // an easier approach in case the name would change in different circumstances

In the above example let’s say the name could change. Instead of hard coding out multiple sentences each with different names, use variables as placeholders. This is probably the simplest example you can get but things get a bit more complicated.

Changing variable values

The beauty of variables is not only that you can declare values within them for easy use across areas of your site but the fact that you can also change the values contained within them after the are first declared. There is a few things you can do, for instance replace the value with a new value or even add new values to the already existent value within the variable. Here’s a simple example of how a variable containing the number 1 can have another value added to it.

$number = 1;

$number = $number + 1; // $number is now two

$number += 1; // this is a shorthand way of writing the same thing, $number is now 2

Types of variable values

As stated before variables can hold different values but that statement doesn’t really cover everything. Variables can hold different values and different types of value. For instance they could hold a number also referred to as an integer, let’s say the number 20. Another variable could hold the number 0.2. This would not be referred to as a number but rather a float, a different type of value. Variables can also hold text or text with variables within also referred to as strings. But they can hold multiple values also the form of arrays which are basically a load of different values grouped together. Because of the versatility and flexibility of variables you can move a lot of different information around your scripts easily while maintaining an easy way of manipulating the value(s) inside the variable at the top level.

$number = 1;  // declaring a variable holding an integer

$float = 0.01; // declaring a variable holding an float

$string = "one"; // declaring a variable holding a string

$array = array('one', 'two', 'three'); // declaring a variable holding an array

A variable or an object property?

Class variables provide you even more flexibility into how variables are used within classes and in areas outside those classes such as child classes or outside of class files altogether. You will come into situations where you may want the variable only available within a particular class. This is easily done by declaring the variable within the class as a private variable. This means the variable is only available within the scope of the class and cannot be called outside the class. This gives you protection from accidentally changing a variables value outside of a class. It also means you can use this variable name in other classes without causing an issue. Variables can also be declared as protected. This means the variable is only available within this class and its child classes. Child classes are basically an extension of a class and can use the parent classes’ variables and functions if required. Public on the other hand means the variable is available anywhere. Within classes, child classes and outside of classes. This is very handy as you can build things within classes, assign them to variables, and then call them on your web page. In classes a variable is not actually referred to as a variable but rather a property. From a class you create an object by declaring a variable holding a new instance of the class and this object can have access to some or all of the classes’ variables / properties. It can also have access to some or all of the class functions / methods.

class test
   private $privateVar = "private";

   protected $privateVar = "private";

   public $publicVar = "public";

$test = new test(); // creating the $test object

echo $test->privateVar; // would output an error as the property is not available outside the class

echo $test->protectedVar; // would output an error as the property is not available outside the class and child classes

echo $test->publicVar; // would output public

The difference between calling variables declared within classes is you don’t just call them by the usual dollar sign followed by the variable name but rather you call the object ($test) then refer to the variable name without the dollar sign and prefix it with a ->. In the above example hopefully you get a simple idea of how you can protect and call variables. It does get far more complicated however. You can create objects inside other classes and multiple instances of the same class.

Variable scope

Different situations create different ways of how your classes and variable interact with one another and one of the most important things you have to understand is variable scope and what extant they are accessible from. Variables inside functions are generally limited to inside that function. Because of this fact you can use the same names for variables within different functions if they are used for holding similar things and these variables will not clash with each other.

function one()
   $num = 1; // a num variable only available within this function

function two()
   $num = 2; // another num variable but only available within this function

$num = 3; // yet another num variable and separate from the other num variables

Although these variables are named the same they are in fact completely separate variables. The reason being that each num variable is only available within the scope of the function and not outside so there is no clash with the variable in the next function. In fact if there was a num variable just outside the function there would be no clash either as the num variable in each function is available only within that function. If in the example above however a num variable was created in the same scope as the num variable equal to 3 this would happen.

$num = 3; // the last variable above

$num = 4; // this however changes the num variable from 3 to 4 as they are the same scope

The variables in each individual function can be manipulated within the function scope the same as the above example. For instance the individual num variable can be change in the first and second function separately to produce different values confined to each function.

function one()
    $num = 1 // declare variable

    $num = 2 // number is now 2 as you are changing the variable in the same scope, e.g. within the function. The num variables in other scopes don’t change e.g. in the second function and outside the function.