What is Variable in PHP?

Variable is a user defined name. which is user for storing the value for temporary. some rules for defined variable in php.

  • in php, variable is defined with $ sign with variable name.
  • variable may captial or small.
  • variable names are case sensitive
  • Assignment Operator (=) is used to assign value to a php variable. eg $x = 10
  • Syntax :

    File name index1.php

    $x = 5;
    $y = 10;
    $sum = $x + $y;
    echo $sum;
    $firstname = "mahtab";
    $lastname = 'alam';

    Example :

    File name index1.php

    <?php
    $str="mahtab";
    $x=100;
    $y=3.14;
    echo "My Name is $str";
    echo "Value of x $x";
    echo "Value of $y";
    ?>

    Output :-

    My Name is mahtab
    Value of x 100
    Vlaue of y 3.14
    Here $str, $x and $y is the variable.

    Variable Declaration Rules in php :-

    File Name :

  • php variable starts with the $ sign. eg:- $x
  • PHP variables must start with letter or underscore only.
  • PHP variable can't be start with numbers and special symbols.
  • PHP variable name can only contain only alpha-numeric characters and underscores. eg:- (A-z, 0-9, and _ )
  • PHP Variable are case-sensitive (such as $sana and $SANA are two different variables because both variables are case sensitive.)
  • A PHP variable name can't contain spaces. eg. $emp name = "Sana Mahtab";

  • Note :-

    File Name :

    Variable value can be change during the execution of the php program. so it is called variable.

    PHP is a loosely typed language.

    PHP is a loosely typed language, it means PHP automatically converts the variable to its correct data type.


    variable Scope :

    The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope.

    File name index1.php

    <?php
    $a = 1; /* global scope */

    function test()
    {
    echo $a; /* reference to local scope variable */
    }

    test();
    ?>

    output :-

    Notice: Undefined variable: a in D:\xampp\htdocs\var\index1.php on line 6

    This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope


    The global keyword

    File name index1.php

    <?php
    $a = 1;
    $b = 2;

    function Sum()
    {
    global $a, $b;

    $b = $a + $b;
    }

    Sum();
    echo $b; // output 3
    ?>

    File name index1.php

    <?php
    function Sum()
    {
    global $a=1; global $b=2;
    // here error, we can't declaere global variable in this way. within function
    $b = $a + $b;
    }

    Sum();
    echo $b; ?>

    GLOBALS

    A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

    The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

    File name index1.php

    <?php
    $a = 1;
    $b = 2;

    function Sum()
    {
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
    }

    Sum();
    echo $b;
    output : 3
    ?>

    Note :

    Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.

    static variable

    Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope

    <?php
    function test()
    {
    static $a = 0;
    echo $a;
    $a++;
    }
    test();
    test();
    test();
    test();
    ?>
    output: 0 1 2 3
    Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it.

    Static variables with recursive functions

    Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion.

    <?php
    function test()
    {
    static $count = 0;

    $count++;
    echo $count;
    if ($count < 10) {
    test();
    }
    $count--;
    }
    test();


    ?>
    output: 12345678910

    what is difference between $(single doller) and $$(double doller).

    The $k is the normal variable that stores any value.

    The $$k is a reference variable that stores the value which can be accessed by using $ symbol before $k value.

    $k = "sana";
    $$k = "hello mahi";
    echo "$k ";
    echo "$$k;";
    echo $sana;



    ########################


    <?php
    $k = "sana";
    $$k = 700;
    echo $k."<br/>";
    echo $$k."<br/>";
    echo $sana;
    ?>

    Output :-

    sana
    hello mahi
    hello mahi



    sana
    700
    700





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here