Site icon Bootsity

All about variables and constants

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:

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:

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:

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.

Exit mobile version