PHP | Variables

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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// 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";  
  
?>

chevron_right


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.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    $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 \n";
    }
      
    local_var();
      
    // $num outside function local_var() is a 
    // completely different Variable than that of 
    // inside local_var()
    echo "Variable num outside local_var() is $num \n";
      
    ?>

    chevron_right

    
    

    Output:

    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.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    $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 \n";
    }
      
    global_var();
      
    echo "Variable num outside function : $num \n";
      
    ?>

    chevron_right

    
    

    Output:

    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.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // function to demonstrate static variables
    function static_var()
    {   
        // static variable
        static $num = 5;
        $sum = 2;
          
        $sum++;
        $num++;
          
        echo $num, "\n";
        echo $sum, "\n";
    }
      
    // first function call
    static_var();
      
    // second function call
    static_var();
      
    ?>

    chevron_right

    
    

    Output:

    6
    3
    7
    3
    

    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.

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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



My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.