PHP | Variables


Variables in a program are used to store some values or data that can be used later in a program. PHP has its own way of declaring and storing variables.
There are few rules, that needs to be followed and facts that need to be kept in mind while dealing with variables in PHP:

  • Any variables declared in PHP must begin with a dollar sign ($), followed by the variable name.
  • A variable can have long descriptive names (like $factorial, $even_nos) or short names (like $n or $f or $x)
  • A variable name can only contain alphanumeric characters and underscores (i.e., ‘a-z’, ‘A-Z’, ‘0-9 and ‘_’) in their name.
  • Assignment of variables are done with assignment operator, “equal to (=)”. The variable names are on the left of equal and the expression or values are to the right of the assignment operator ‘=’.
  • One must keep in mind that variable names in PHP names must start with a letter or underscore and no numbers.
  • PHP is a loosely typed language and we do not require to declare the data types of variables, rather PHP assumes it automatically by analyzing the values. Same happens while conversion.
  • PHP variables are case-sensitive, i.e., $sum and $SUM are treated differently.


// These are all valid declarations
$val = 5;
$val2 = 2;
$x_Y = "gfg";
$_X = "GeeksforGeeks";
// This is an invalid declaration as it 
// begins with a number
$10_ val = 56; 
// This is also invalid as it contains 
// special character other than _
$f.d = "num";  

Variable Scopes

Scope of a variable is defined as its extent in program within which it can be accessed, i.e. the scope of a variable is the portion of the program with in which it is visible or can be accessed.
Depending on the scopes, PHP has three variable scopes:

  1. Local variables: The variables declared within a function are called local variables to that function and has its scope only in that particular function. In simple words it cannot be accessed outside that function. Any declaration of a variable outside the function with same name as that of the one within the function is a complete different variable. We will learn about functions in details in later articles. For now consider a function as a block of statements.

    $num = 60;
    function local_var()
        // This $num is local to this function
        // the variable $num outside this function
        // is a completely different variable
        $num = 50;
        echo "local num = $num ";
    // $num outside function local_var() is a 
    // completely different Variable than that of 
    // inside local_var()
    echo "Variable num outside local_var() is $num ";


    local num = 50 
    Variable num outside local_var() is 60 
  2. Global variables: The variables declared outside a function are called global variables. These variables can be accessed directly outside a function. To get access within a function we need to use the “global” keyword before the variable to refer to the global variable.

    $num = 20;
    // function to demonstrate use of global variable
    function global_var()
        // we have to use global keyword before 
        // the variable $num to access within 
        // the function
        global $num;
        echo "Variable num inside function : $num ";
    echo "Variable num outside function : $num ";


    Variable num inside function : 20 
    Variable num outside function : 20 
  3. Static variable: It is the characteristic of PHP to delete the variable, ones it completes its execution and the memory is freed. But sometimes we need to store the variables even after the completion of function execution. To do this we use static keyword and the variables are then called as static variables.

    // function to demonstrate static variables
    function static_var()
        // static variable
        static $num = 5;
        $sum = 2;
        echo $num, " ";
        echo $sum, " ";
    // first function call
    // second function call



    You must have noticed that $num regularly increments even after the first function call but $sum doesn’t. This is because $sum is not static and it’s memory is freed after execution of first function call.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter