All about variables and constants

(Last Updated On: November 6, 2018)

1. Introduction

In the first post of this series, we learned how to write a hello world program in PHP. In this second post, we are exploring about variables and constants in PHP.
In simple terms, variables are storage areas in which we store data. In the storage areas of variables data may “variate” means we can change data. The special type of varibles where we cannot change data are known as constants. In PHP, variables always start with $. Let’s have a deeper look on variables and constants.

2. Defining variables

Defining a variable means that we are saying to the PHP interpreter that – here is our storage area and we are saving a particular value in it. A variable is defined as:

$planet = "earth";

In the above line we are saying to interpreter that $planet is our storage area and we are storing “earth” value.

3. Types of data

In storage areas or variables, we can store different types of data:

  • Integers : Integers are non-decimal numbers between -(2^31) and +(2^31) for 32-bit builds of PHP (First bit is used to denote whether a number is negative or positive). Similarly, ranges can be computed for 64-bit builds. Example: $i = 10;
  • Floats : This is the name for numbers with a decimal point or in exponential form. Like: $f = 10.01
  • Strings : This simply means text. Text can be a character or word or sentence. Strings are declared as: $str = ‘this is moon’;
  • Boolean : Booleans are used to store true/false values.
  • Arrays : Arrays are variables that store several values. We will talk about them in detail later.
  • Objects : Objects store both data and information on how to process it. Objects are combinations of multiple data types.
  • NULL : A variable that is NULL doesn’t have any value. You can see NULL as empty.

We should note that there is no need to declare PHP variables. PHP automatically takes the type of data that is stored in the variable. This property is known as Loosely Typed Language.

4. Variable scope

The scope of variable answers – after defining our variables, where can we use that variable? Scope provides information like you can use variable in a particular function or anywhere in your program. In PHP, we have three types of variable scopes – local, global and static:

4.1 Local variable scope

When a variable is defined inside a function, it have local scope inside that function. It means, we cannot access the variable outside that function. Consider:

function earthAtmo{
    $element = "water";
    // $element is accessible inside earthAtmo function
}

4.2 Global variable scope

When a variable is defined outside all function, it can be accessed from anywhere in the program and hence named as global scope. See:

$lightSource = "sun";

function earthAtmo{
    // $lightSource is accessible inside earthAtmo function
}

function marsAtmo{
    // $lightSource is accessible inside marsAtmo function
}

// $lightSource is also accessible here

4.3 Static variable scope

When a program is running, all the local variables are removed from the memory as soon as function call is complete. Sometimes we need local variables even after a function completes its execution. In such cases we can scope our variable as static and it will persist in memory even after function call. We declare static variable as:

function earthAtmo{
    static $life = true;
}
// $life will not be cleared from memory even after function earthAtmo execution is complete

5. Predefined variables or superglobals

PHP have some inbuilt predefined global variables known as superglobals. These variables can be accessed from anywhere in our program. Some important superglobals are discussed:

  • $GLOBALS : This superglobal contains the information about all the global variables used in our program. It can also be used to access the global variables.
  • $_SERVER : contains information about server, it’s filesystem and location of different resources.
  • $_GET : it have information about request query parameters that are passed in an URL.
  • $_POST : collects all the information from request body when the request type is POST
  • $_REQUEST : this can be seen as combination of $_GET and $_POST and few other information about request.

6. Defining constants

Constants are variables having fixed values. It means constants are a special type of variables which don’t allow changing of values. You can define your constant as:

define("PLANET", "EARTH");

echo PLANET; // prints EARTH

7. Predefined constants

There are some pre-defined constants in PHP. These predefined constants come handy for various thing in our programs. Let’s see them:

  • __LINE__ : line of the current file.
  • __FILE__ : current filename.
  • __DIR__ : directory of current file.
  • __FUNCTION__ : name of the current function.
  • __CLASS__ : name of current class.
  • __TRAIT__ : trait’s name with namespaces.
  • __METHOD__ : class’ method name.
  • __NAMESPACE__ : namespace information

8. Conclusion

In this second post of the series – PHP Back to Basics, we learned about variables – how we can declare them, use them in our program and what are readily available variables that we can use. Then we discussed about constants and predefined constants. In the next post in this series, we will discuss about how we can put logic in our program using conditions.

LEAVE A REPLY

Please enter your comment!
Please enter your name here