Image

PHP - Variable - Variable

Variables

Variable is name given to the storage area and its value change during programming execution is known as variable. The variable are use to store data. like number of arrays.

PHP variables begin with a dollar sign ($) as shown below.

Syntax
$var_name = "Value";

Variable TypeExplanation
booleanTrue or false
arrayTList of items
objectInstall of a class

Variable Names (Identifiers )
  • consist of letters, digits, underscores and dollar signs
  • cannot begin with a digit
  • are case sensitive
  • Code Sample: HelloVariables.php
    <?php
    $Greeting = 'Hello World!';
    ?>
    <html>
    <head>
    <title><?php echo $Greeting; ?></title>
    </head>
    <body>
    <?php
    echo $Greeting;
    ?>
    </body>
    </html>
    O/P
    Hello World!

    Rules for PHP variables
    1. A variable name start with a letter or the underscore character
    2. A variable name can not start with number
    3. A variable start with the $ sign, followed by the name of the variable
    4. Variable name are case-sensitive ($age and $AGE are two different variable)
    5. A variable name can only contain alpha-numeric characters and underscore (A-z, 0-9, and _)

    Variable Scope

    A variable's scope determines the locations from which the variable can be accessed.

    PHP variables are either super global, global, or local.
    Super global

    Superglobal variables are predefined arrays, including $_POST and $_GET. They are accessible from anywhere on the page.

    The complete list of super global is shown below.
  • $_GET - variables passed into a page on the query string.
  • $_POST - variables passed into a page through a form using the post method.
  • $_SERVER - server environment variables (e.g, $_SERVER['HTTP_REFERER'] returns the URL of the referring page).
  • $_COOKIE - cookie variables.
  • $_FILES - variables containing information about uploaded files.
  • $_ENV - PHP environment variables (e.g, $_ENV['HTTP_HOST'] returns the name of the host server.
  • $_REQUEST - variables passed into a page through forms, the query string and cookies.
  • $_SESSION - session variables.

  • Global variables

    Global variables are visible throughout the script in which they are declared. However, they are not visible within functions in the script unless they are re-declared within the function as global variables


    Function

    Variables in the function scope are called local variables. Local variables are local to the function in which they are declared.


    Constants

    Constants are like variables except that, once assigned a value, they cannot be changed. Constants are created using the define() function and by convention (but not by rule) are in all uppercase letters. Constants can be accessed from anywhere on the page.

    A constant Is a identifier for a simple value. As the name suggest, that value cannot change during execution of the script.

    Syntax
    define('CONST_NAME',VALUE);