PHP Variable Types

Variable is a container for storing programming data. The stored data can be used later at multiple locations in the program file by calling the variable name. It is a very common feature of a programming language. Almost all modern programming language built-in variable feature. PHP variable is the main way of storing data for later uses in the program. In PHP there are some rules and regulations you have to follow while declaring or defining a variable. Let’s learn the rules by point.

Declaring PHP Variable 

  • Declaring any variable in PHP must begin with a dollars sign ($) followed by the variable name.
  • A variable can only contain alphanumeric character (e:g: a-z, A-Z, 0-9 and underscore “_”)
  • A variable must starts with (a-z, A-Z or underscore “_”). You can not use a number at the start of a variable otherwise, it will not be a valid variable.
  • In PHP, the variable has no character limitation. You can use a number of characters in a variable as much as you can. But always try to keep the variable name small. It will help to manipulate the code easily.
  • PHP variable is loosely typed scripting language. While there are many kinds of variables you can declare but you don’t have to mention which kind of variable you are declaring. PHP automatically understands that.
  • PHP variable is case sensitive. $mango and $Mango act as 2 different variables.

Let’s try to understand by example which variable is valid and which is invalid one:

<?php
$val = 5; //valid
$2val = 2;  //invalid
$x_Y = "gfg";   //valid
$_X = "GeeksforGeeks";  //valid

In the example above, $val is an integer and valid variable. We will learn in details about integer variable in the later chapter. But $2val is not a valid variable. Because In PHP variable can’t be started with any numbers. Then $x_Y variable is also a valid and string variable. It started with an alphanumeric small latter and contains an underscore. That is actually valid. We will learn more about the string variable in the later chapter. Again $_X is also a string variable and also a valid variable because while declaring “_” can be used at the start. ?>

PHP Variable Scope

When you declare a variable, by default you can access the variable in the whole file. Means wherever you want to use the stored value of a variable, simply you can call the variable by its name. But you can make code block privileges for accessing a specific number of code-block in a file and some other factors (e:g: inside function access, outside of a function access etc.).

There are three kinds of variable scope in PHP. Local variables, global variables and static variables. Let’s talk about them in details:

1.Local Variables: When a function is written inside a function. The variable then only accessed within the function. Means, the function is not accessed from outside of the function. We don’t have to know what is a function in PHP right now. For now, remember that local function is written inside a specific function only for accessing from the function. In the later chapter, we will learn the more about function and uses of the local variable as well. Let’s see an example for demonstration purpose:

<?php
$var_type = "Global Variable.";  //It's a global variable, can't be accessed from any of function below.
function local_variable()
{   
    $var_type = "Local Variable.";
    // This $var_type is local variable to this function
   // the variable $var_type outside this function
   // is a completely different variable
    echo "1st Executed from ".$var_type."\n";
}
local_var();
echo "2nd Executed from ".$var_type."\n";
?>

Output:

1st Executed from Local Variable
2nd Executed from Global Variable

Text the code using your localhost or live server. Don’t use an online IDE.

2.Global Variable: Variables that declared from the outside of a function are called global variable. If you want to use a global variable inside a specific function then you have to use “global” keyword to include the variable in the function. An example will clear it. Let’s see an example:

<?php
$motto = "Hello, world!";
     //Creating a function for checking global variable.
function global_var()
{
    echo "Every programmer starts with ".$motto;
}
global_var();
?>

Output:

Every programmer starts with

You can see the stored data of the variable $motto is not executing. Although here $motto is a global variable. But as the definition of the global variable says, a global variable can be used in the whole file. So in this case, why the variable didn’t work? Actually, we have to use the global keyword in order to access the global variable. Let’s see another example:

<?php
$motto = "Hello, world!";
     //Creating a function for checking global variable.
function global_var()
{
    //here you have to use the global keyword
    global $motto
    echo "Every programmer starts with ".$motto;
}
global_var();
?>

Now the Output will be:

Every programmer starts with Hello, World!

You can see, after using the global keyword the variable is working inside the function. In the same way, Every time you want to use a global variable, you must have to declare the global keyword to the corresponding function.

3.Static Function: 

 

Reference: http://php.net