Php Variables| Scope Variables Easy to Learn

Php Variables| Scope Variables Easy to Learn
Php Variables| Scope Variables Easy to Learn

Php Variables Learn Php Programming

    PHP Variables: A variable in Php is the name of the memory location that holds data. A variable is temporary storage that is used to store data temporarily. A Variable is a container.

    In Php, a variable is declared using the $ sign followed by the variable name.


    What are the seven rules of writing variables?

    1. It must start with a dollar sign.

    2. There is no need for the data type(int, float, char) to define variables.

    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 a variable name.


    Syntax of declaring a variable in Php is given below:

    $variablename=value; 


    How To Use Concatenate Variable Using dot(.) operator in Php?

    <php

    $s="codeswithbaby";

    echo "I like ".$s;

    ?>

    *****OUTPUT*****

    I like codeswithbaby


    How do Declaring string, integer, and float variables in Php?

    <?php

    $str="Hello Php";

    $x=100;

    $y=40.6;

    echo "A String is: $str <br/>";

    echo "An Integer is: $x <br/>";

    echo "Float is: $y <br/>"; ?>  


    *****Output*****

    A string is: Hello Php

    An integer is: 100

    The float is: 40.6


    What Types of Php Variables and Scopes

    Php variable is defined as its extent in a program in which it can be accessed, the scope of a variable is the part of the program within which it is visible or can be accessed.

    1. Local Variable

    2. Global Variable

    3. Static Variable


    Local Variable in C Language

    Local variables: A variable declared inside the body of the method or constructor is called a local variable. A local variable can be used only inside the method/function in which it is declared. A local variable can be a static variable. Any declaration of a variable outside the function with the same name as that of the one within the function is a completely different variable.


    Php Variable Scope| Local Scope Variables 

    <?php

    $num = 50;

    function local_var()

    {

              $num = 100;

              echo "Local Variable Value:$num \n";

    }

    local_var();

    echo "Variable outside the value:$num \n";

    ?>


    *****OUTPUT*****

    Local Variable Value:100

    Variable outside the value:50



    How To Use Local Variable in Php?

    <?php

    function fundata()

    {

    $a=100;

    $b=200;

    echo "The Value of a is $a and b is $b";

    }

    fundata();

    ?>


    *****OUTPUT*****

    The value of a is 100 and b is 200


    Global Variable in C Language

    Global Variable: A variable that is declared outside the body of the method or constructor is called a global variable. A global variable declared outside the function is called the global variable. These variables can be accessed directly outside a function. The global variable can be accessed by the global “Keyword” used in the global variable.


    Php Variable Scope|Global Scope Variables 

    <?php

    $num = 100;

    function global_var()

    {

              global $num;      

              echo "Variable value inside function : $num \n";

    }

    global_var();

    echo "Variable value outside function : $num \n";

    ?>


    *****OUTPUT*****

    Variable value inside function:100

    Variable value outside function:100


    How To Use Global Variable in Php?

    <?php

    $a=10;

    $b=20;

    function fundata()

    {

    $a=100;

    $b=200;

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

    }

    fundata();

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

    ?>


    *****OUTPUT*****

    Inside function: The value of a is 100 and b is 200

    Outside function: The value of a is 10 and b is 20


    Static Variable in C Language

    Static Variable: A variable that is declared with a static keyword is called a static variable. The static variable is stored in the static memory. Static variables are created when the program starts and destroyed when the program stops. static variables even after the completion of function execution. We can use the static keywords and the variables are then called static variables. The static variables free the memory and stored the next values.

    Php Variable Scope|Static Scope Variables

    <?php

    function static_var()

    {

              static $x = 10;

              $y = 5;

              $y++;

              $x++;         

              echo " First call static variable:\n",$x;

              echo " Normal Variable:\n",$y;

    }

    static_var();

    static_var();

    ?>


    *****OUTPUT*****

    First call static variable: 11

    Normal variable: 6

    Second call static variable: 12

    Normal variable: 6


    How To Use Normal Variable in Php?

    <?php

    function fundata()

    {

    $a=100;

    echo "$a<br>";

    $a++;

    }

    fundata();

    fundata();

    fundata();

    ?>


    *****OUTPUT*****

    100

    100

    100


    How To Use With Static Variable in Php?

    <?php

    function fundata()

    {

    static $a=100;

    echo "$a<br>";

    $a++;

    }

    fundata();

    fundata();

    fundata();

    ?>


    *****OUTPUT*****

    100

    101

    102


    Example of Php Valid Variable

    <?php

    $a="Hello Php";

    //letter (valid)

    $_b="Hello World";

    //underscore (valid)

    echo "$a <br/>

    $_b";

    ?> 


    *****OUTPUT*****

    Hello Php

    Hello World

     

    Read Also Post Other Programming Languages

    👉Java Variables


    👉C Variables


    👉C++ Variables


    👉Python Variables




    Post a Comment

    0 Comments
    * Please Don't Spam Here. All the Comments are Reviewed by Admin.