Variables in PHP

Share this post on:

What is a variable?

A Variables is a container placeholder whatever you want to call it elements that we can use to store data and then use that data by the variable name. So we can refer to that data by using the variables name.

Note: When you assign a text value to a variable, put quotes around the value.

Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

Think of variables as containers for storing data.

Remember that PHP variable names are case-sensitive!

Naming Conventions for PHP Variables

These are the following rules for naming a PHP variable:

  1. A variable name cannot contain spaces.
  2. A variable name cannot start with a number.
  3. A variable name must start with a letter or the underscore character _.
  4. All variables in PHP start with a $ sign, followed by the name of the variable.
  5. A variable name in PHP can only contain alpha-numeric characters and underscores (A-z0-9, and _).
Example
<?php
$name = 'Kishan Sharma';
$number = 10;
$NUMBER = 40;
$NUMBeR = 10;
$Number_list = 50.05;
// $num-ber = 100; this tyoe is not use in here

echo $name;
echo "<br>";
echo $number;
echo "<br>";
echo $NUMBER;
echo "<br>";
echo $Number_list;
echo "<br>";
echo $NUMBER . $NUMBeR;
echo "<br>";
echo $number ." ".$Number_list;
?>
Output
Kishan Sharma
10
40
50.05
4010
10 50.05

PHP Variable Scope

The scope of a variable is defined as its range in the program under which it can be accessed. In other words, “The scope of a variable is the portion of the program within which it is defined and can be accessed.”

PHP has three types of variable scopes:

  1. Local variable
  2. Global variable
  3. Static variable

Local variable

The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.

A variable declaration outside the function with the same name is completely different from the variable declared inside the function. Let’s understand the local variables with the help of an example:

Example

<?php  
    function local_var()  
    {  
        $num = 45;  //local variable  
        echo "Local variable declared inside the function is: ". $num;  
    }  
    local_var();  
?>

Output

Local variable declared inside the function is: 45

Global variable

The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.

<?php  
    $name = "Vikram Sharma";        //Global Variable  
    function global_var()  
    {  
        global $name;  
        echo "Variable inside the function: ". $name;  
        echo "</br>";  
    }  
    global_var();  
    echo "Variable outside the function: ". $name;  
?>
Output
Variable inside the function: Vikram Sharma
Variable outside the function: Vikram Sharma

Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate an error that the variable is undefined.

Static variable

It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. Therefore, another important feature of variable scoping is a static variable. We use the static keyword before the variable to define a variable, and this variable is called a static variable.

Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope.

<?php  
    function static_var()  
    {  
        static $num1 = 3;       //static variable  
        $num2 = 6;          //Non-static variable  
        //increment in non-static variable  
        $num1++;  
        //increment in static variable  
        $num2++;  
        echo "Static: " .$num1 ."</br>";  
        echo "Non-static: " .$num2 ."</br>";  
    }  
      
//first function call  
    static_var();  
  
    //second function call  
    static_var();  
?>
Output
Static: 4
Non-static: 7
Static: 5
Non-static: 7

You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This is why because $num1 is not a static variable, so it freed its memory after the execution of each function call.

Share this post on:

Leave a Reply