How to declare variable in Php

Php Variables: Variables is a name of storage space which is used to store data. Its value may be changed. It always contains the last value stored to it. Variables is a container which holds the value. Variable in PHP starts with dollar($) sign.

Variables are tools used to store information for later use. Think of variables as placeholders for any amount of data. This data Can then be used throughout your

code with ease. Variables are like the touchscreens of our modern devices, you can access the data with your fingertips. Just like touchscreens, variables offer a direct means of interaction with the information that is stored within them.

After you learn PHP basics, you will see dollar signs not only in your dreams but also on your screen, as this symbol ($) is used to mark variables.
A PHP variable is similar to a container that is used to store information (data). This data usually consists of letters or numbers, but a variable can also exist without storing any value.

A variable can store a value of any type such as a string (textual data in programming is referred to as strings), a number, or even an array, that we shall learn more about as we progress.

A variable has a name and is associated with a value. To define a variable, you use the following syntax,
$variable_name = value; Following are some of the rules that you might need to follow when defining a variable, The variable name must start with the dollar sign ($).

Note: That variable names in PHP are case-sensitive, this means $name, $Name, and $NAME are three separate variables.


  • We do not need to declare the data types of the variables because PHP is a weakly typed language.
  • It evaluates the values automatically and converts them to the appropriate datatype.
  • A variable can be utilised repeatedly after being declared in the code.
  • To assign a value to a variable, use the assignment operator (=).
PHP Variables Learn| Types Variables| Declare Variables
Best PHP Variables Learn| Types Variables| Declare Variables

    What is declared syntax?

    $variablename=value;


    What are variables in PHP, explain with example?

    <?php

    $x=100;

    $x=50;

    $x=250;

    echo "The value of x is:".$x;

    ?>

    **********OUTPUT**********

    The value of x is:250


    What is string in PHP with example?

    <?php

    $str="Learn Coding";

    echo "I like $str";

    ?>

    **********OUTPUT**********

    I like Learn Coding


    Can you concatenate strings in PHP?

    <?php

    $str="Learn Coding";

    echo "I like ".$str;

    ?>

    **********OUTPUT**********

    I like Learn Coding

    What are the rules to declare variable in PHP?

    1. It must start with the dollar sign.

    2. There is no need of data type (int, float, char) to define a variable.

    3. Variables can, but do not need, to be declared before assignment.

    4. After the dollar sign, the first letter of a variable should be alphabet or underscore(_).

    5. After the dollar sign, the first letter of a variable should not be a digit.

    6. After the first character, it may be a combination of alphabets and digits.

    7. Blank spaces are not allowed in variable name.

    8. A variable must start with a dollar ($) sign, followed by the variable name.

    9. It can only contain alphanumeric character and underscore (A-z, 0-9, _).

    10. A variable name must start with a letter or underscore (_) character.

    11. A PHP variable name cannot contain spaces.

    12. One thing to be kept in mind that the variable name cannot start with a number or special symbols.

    13. PHP variables are case-sensitive, so $name and $NAME both are treated as different variable.

    PHP Variable: Declaring string, integer, and float

    Let's see the example to store string, integer, and float values in PHP variables.

    <?php  

    $str="Learn Coding";  

    $x=100;  

    $y=48.6;  

    echo"string is: $str <br/>";  

    echo "integer is: $x <br/>";  

    echo "float is: $y <br/>";  

    ?>  


    **********OUTPUT**********

    string is: Learn Coding

    integer is: 100

    float is: 48.6



    PHP Variable: Sum of two variables

    <?php  

    $x=100;  

    $y=200;  

    $z=$x+$y;  

    echo("Addition of two number:".$z);  

    ?>  

    **********OUTPUT**********

    Addition of two number:300


    What are the four types of variable scopes?

    Php Variable Scope: The scope of a variable is defined as its range in the program under which it can be accessed. In other words, "The scope of a variable is the portion of the program within which it is defined and can be accessed."

    What is the scope of variable in PHP?
    1. Local variable
    2. Global variable
    3. Static variable

    What is a local variable in PHP?

    Local Variable: The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.

    A variable declaration outside the function with the same name is completely different from the variable declared inside the function.

    A variable declared inside the body of the method or constructor is called a local variable.
    A local variable can be used only inside that method/function in which it is declared.
    A local variable can be a static variable.

    How to make local variable in PHP

    <?php  

        function local_var()  

        {  

            $num = 45;  //local variable  

            echo "Local variable declared inside the function is: ". $num;  

        }  

        local_var();  

    ?>  

    **********OUTPUT**********

    Local variable declared inside the function is: 45


    How to declare local variable in PHP

    <?php

    function mydata()

    {

    $x=100;

    $y=200;

    echo "The value of x is $x and y is $y";

    }


    mydata();

    ?>

    **********OUTPUT**********

    The value of x is 100 and y is 200


    What is the best way to handle global variables in PHP

    Global variable: A variable which is declared outside the body of the method or constructor is called a global variable. The global variables are the variables that are declared outside the function. 

    These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. 

    However, these variables can be directly accessed or used outside the function without any keyword. Therefore, there is no need to use any keyword to access a global variable outside the function.

    Php global variable in function

    <?php

    $x=100;

    $y=200;

    function mydata()

    {

    $x=10;

    $y=20;

    echo "<b>Inside function:</b> The value of x is $x and y is $y <br>";

    }

    mydata();

    echo "<b>Outside function:</b>The value of x is $x and y is $y ";

    ?>


    **********OUTPUT**********

    Inside function: The value of x is 10 and y is 20 

    Outside function:The value of x is 100 and y is 200


    What is use of global variable in PHP

    <?php  

        $name = "Ravi Kumar";        //Global Variable  

        function global_var()  

        {  

            global $name;  

            echo "Variable inside the function: ". $name;  

            echo "</br>";  

        }  

        global_var();  

        echo "Variable outside the function: ". $name;  

    ?>  

    **********OUTPUT**********

    Variable inside the function: Ravi Kumar

    Variable outside the function: Ravi Kumar

    Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate an error that the variable is undefined.


    What is a static variable in PHP

    Static Variable: A variable which is declared with a static keyword is called a static variable. Static variable is stored in the static memory.
    Static variables are created when the program starts and destroyed when the program stops.
    It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. 

    Therefore, another important feature of variable scoping is static variable. We use the static keyword before the variable to define a variable, and this variable is called as static variable.

    Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope.

    Php access static variable in function

    <?php

    function mydata()

    {

    $x=100;

    echo "$x<br>";

    $x++;

    }

    mydata();

    mydata();

    mydata();

    ?>

    **********OUTPUT**********

    100

    100

    100


    What is static variable with example

    <?php

    function myFunction()

    {

    static $x=10;

    echo "$x<br>";

    $x++;

    }

    mydata();

    mydata();

    mydata();

    ?>


    **********OUTPUT**********

    10

    11

    12


    Php static variables in function

    <?php  

        function static_var()  

        {  

            static $num1 = 3;       //static variable  

            $num2 = 6;          //Non-static variable  

         

            $num1++;  

         

            $num2++;  

            echo "Static: " .$num1 ."</br>";  

            echo "Non-static: " .$num2 ."</br>";  

        }  


     static_var();   

     static_var();  

    ?> 


    **********OUTPUT**********

    Static: 4

    Non-static: 7

    Static: 5

    Non-static: 7


    How can use local variable as global in PHP?

    <?php

    $x=100;

    $y=50;

    function mydata()

    {

    global $x,$y;

    echo "<b>Inside function:</b> The value of x is $x and y is $y <br>";

    }

    mydata();

    echo "<b>Outside function:</b>The value of x is $x and y is $y ";

    ?>


    **********OUTPUT**********

    Inside function: The value of x is 100 and y is 50 

    Outside function:The value of x is 100 and y is 50


    PHP Variable Scope|Local Scope Vs Global Scope Variables 

    <?php

    $x=50;

    $y=50;

    function mydata()

    {

    $sum=$GLOBALS['x']+$GLOBALS['y'];

    echo "<b>Inside function:</b> Sum=$sum";

    }

    mydata();

    $sum=$x+$y;

    echo "<b>Outside function:</b> Sum=$sum";

    ?>


    **********OUTPUT**********

    Inside function: Sum=100

    Outside function: Sum=100


    What is PHP $ and $$ variables?

    If two variables, local and global, have the same name, then the local variable has higher priority than the global variable inside the function.

    <?php  

        $num1 = 50;      //global variable  

        $num2 = 10;     //global variable  

        function global_data()  

        {  

                $sum = $GLOBALS['num1'] + $GLOBALS['num2'];  

                echo "The Sum of global variables is:" .$sum;  

        }  

        global_data();  

    ?>  


    **********OUTPUT**********

    The sum of global variables is: 60


    What is $_ global in PHP?

    <?php  

        $x = 50;  

        function mydata()  

        {  

            $x = 100;  

            echo "value of x: " .$x;  

        }  

        mydata();  

    ?>  


    **********OUTPUT**********

    Value of x: 100


    PHP $ and $$ Variables: The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc.
    The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.

    How to use $ and && in PHP Example

    <?php  

    $x = "Learn Coding";  

    $$x = 500;  

    echo $x."<br/>";  

    echo $$x."<br/>";  

    echo $abc;  

    ?>  


    **********OUTPUT**********

    Learn Coding

    500

    500

    In the above example, we have assigned a value to the variable x as Learn Coding. Value of reference variable $$x is assigned as 500.

    FAQ Php Variables Question Answer

    Q. 1 Local Variables in PHP?
    Ans: Local Variable: For that function, the variables that are declared inside of it are referred to as local variables. Only the specific function in which they are declared can use these local variables. As a result of their local scope, these variables cannot be accessible from outside the function.

    Q. 2 Global variables in PHP?
    Ans: Global Variables: A variable which is declared outside the body of the method or constructor is called global variables.

    Q. 3 static variables in PHP?
    Ans: Static Variable:Static variables are those that have the static keyword used to declare them. The static memory houses static variables. When a programme begins, static variables are generated, and when the programme ends, they are destroyed.

    Q. 4 Dynamic Variables in PHP?
    Ans: dynamic element The value kept in another variable can be used to name a variable. In other words, the name of one variable appears in the name of another.

    Q. 5 Types of Variables in PHP?
    Ans: Local variable,global variables, static variable and dynamic variables.

    Conclusion: Hello Php programmer you can php variables all method explain in this artical. you can learn every variable with example easy to understand in this post.