"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Friday, January 19, 2024

PHP VARIABLES

 PHP Variable

Variables are like the "containers" for storing information.

A variable can have a short name (like a and b) or a more descriptive name (age, username, user_name).

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

As PHP is a loosely typed language, so we do not need to declare the data types of the variables. It automatically analyzes the values and makes conversions to its correct datatype.

After declaring a variable, it can be reused throughout the code.

Assignment Operator (=) is used to assign the value to a variable.

Syntax of declaring a variable in PHP is given below:

$variablename=value;

Rules for declaring PHP variable:

  • A variable must start with a dollar ($) sign, followed by the variable name.
  • It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
  • A variable name must start with a letter or underscore (_) character.
  • A PHP variable name cannot contain spaces.
  • One thing to be kept in mind that the variable name cannot start with a number or special symbols.
  • PHP variables are case-sensitive, so $name and $NAME both are treated as different variable.

PHP Variable: Declaring string, integer, and float

Variable.php                                        

<?php

$str="hello string";

$x=200;

$y=44.6;

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

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

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

?>

Output

string is: hello string

integer is: 200

float is: 44.6

PHP Variable: Sum of two variables

<?php                 

$x=5;  

$y=6;  

$z=$x+$y;  

echo $z;  

?>  

Output:

11

PHP Variable: case sensitive

In PHP, variable names are case sensitive. So variable name "color" is different from Color, COLOR, COLor etc.

<?php  

$color="red";  

echo "My car is " . $color . "<br>";  

echo "My house is " . $COLOR . "<br>";  

echo "My boat is " . $coLOR . "<br>";  

?>            

Output:

My car is red

Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4

My house is

Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5

My boat is

PHP: Loosely typed language

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

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."

PHP has three types of variable scopes:

  1. Local variable
  2. Global variable
  3. Static variable

Local variable

1.      The variables that are declared within a function are called local variables for that function.

2.      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.

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

4.      Example: File: local_variable1.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

 

File: local_variable2.php

<?php  

    function mytest()  

    {  

        $lang = "PHP";  

        echo "Web development language: " .$lang;  

    }  

    mytest();  

    //using $lang (local variable) outside the function will generate an error  

    echo $lang;  

?>          

Output:

Web development language: PHP
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28

Global variable

1.      The global variables are the variables that are declared outside the function.

2.      These variables can be accessed anywhere in the program.

3.      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.

4.      Therefore there is no need to use any keyword to access a global variable outside the function.

5.      Example: File: global_variable1.php

<?php  

    $name = "Sanaya Sharma";        //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: Sanaya Sharma
Variable outside the function: Sanaya Sharma

File: global_variable2.php

<?php        

    $name = "Sanaya Sharma";        //global variable  

    function global_var()  

    {  

        echo "Variable inside the function: "$name;  

        echo "</br>";  

    }  

    global_var();  

?>  

Output:

Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6
Variable inside the function:

Using $GLOBALS instead of global

1.      Another way to use the global variable inside the function is predefined $GLOBALS array.

2.      Example: File: global_variable3.php

<?php                                

    $num1 = 5;      //global variable  

    $num2 = 13;     //global variable  

    function global_var()  

    {  

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

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

    }  

    global_var();  

?>  

Output:

Sum of global variables is: 18

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

File: global_variable2.php

<?php                   

    $x = 5;  

    function mytest()  

    {  

        $x = 7;  

        echo "value of x: " .$x;  

    }  

    mytest();  

?>  

Output:

Value of x: 7

 

Static variable

1.      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.

2.      We use the static keyword before the variable to define a variable, and this variable is called as static variable.

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

4.      Example: File: static_variable.php

<?php  

    function static_var()  

    {  

        static $num1 = 3;       //static variable  

        $num2 = 6;          //Non-static variable  

        //increment in non-static variable  

        $num1++;  

        //increment in static variable  

        $num2++;  

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

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

    }  

      

//first function call  

    static_var();  

  

    //second function call  

    static_var();  

?>  

Output:

Static: 4
Non-static: 7
Static: 5
Non-static: 7

You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This is why because $num1 is not a static variable, so it freed its memory after the execution of each function call.

 

 

 

 

 

 

No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template