Use of PHP Global Variable

The variable is used to store any value temporarily in the script. Two types of variables are used in any programming language. These are local and global variables. The variables which are accessible anywhere in the script called global variables. That’s mean the value of the global variables can be accessed or modified inside and outside of the function. But if the name of any global variable is the same as any variable declared inside a function there are some ways to recognize the global variable inside the function. Two types of global variables are used in PHP. One is a user-defined global variable and another is a superglobal variable. Some useful superglobal variables are $_GLOBALS, $_SERVER, $_REQUEST, $_GET, $_POST, $_FILES, $_COOKIE and $_SESSION. How the user-defined global variable can be declared, assigned, and changed inside and outside the function have shown in this tutorial.

Syntax

$variable_name = value

‘$’ symbol is used to declare any type of variable in PHP. The rules of declaring the variable name must be followed to declare the variable. Any number or string or NULL value can be assigned as the value of the variable.

Example 1: Declare a simple global variable

The following example shows how the global variable with the string value and the numeric value can be declared and printed in the PHP script. In the script, the $message variable is used to store the string value and the $year variable is used to store the numeric value. These two variables are printed later.

<?php
//Declare a variable with a string value
$message = "Welcome to LinuxHint";
//Print the variable
echo $message."<br />";
//Declare a variable with a number value
$year = 2020;
//Print the variable
echo "The current year is $year";
?>

Output:

The following output will appear after running the above script from the server.

Example 2: Accessing a global variable inside a function using the global keyword

The following script shows one way of using the global variable inside the PHP function. the global variable can’t be recognized inside the PHP function and the variable will treat as a local variable. Here, the global keyword is used with the variable to use the previously defined global variable inside the function named add(). $number is a global variable here. The value of this variable is modified inside and outside the function. The variable is printed inside and outside of the function also to check the change of the global variable.

<?php
//Declare a global variable with number
$number = 10;
//Declare a user-defined function
function add()
{
    //global keyword is used to identify the global variable
    global $number;
    //Add 20 with the global variable
    $number = $number + 20;
    //Print the value of the global variable
    echo "The value of the global variable inside the function is : $number <br/>";
}
add();
//Substract 5 from the global variable
$number = $number - 5;
//Print the value of the global variable
echo "The value of the global variable outside the function is : $number";
?>

Output:

The following output will appear after running the above script from the server. The value of the $number is 10 before calling the function. 20 is added with $number inside the function and the value of $number is printed that is 30. Next, 5 is deducted from the $number outside the function that is 25.

Example 3: Accessing a global variable inside a function using $GLOBALS array

The following example shows another way of using the global variable inside the function. Here, the $_GLOBALS[] array is used to identify the global variable inside the function. In the script, three global variables are declared. Two variables named $value1 and $value2 are initialized with the string values and the variable $value is undefined that is initialized later inside and outside of the function. The values of $value1 and $value2 are combined and stored in $value inside the function and printed. Next, the value of $value1 is combined with another string value and stored in $value outside the function.

<?php
//Declare three global variables
$value;
$value1 = 'PHP';
$value2 = ' is a scripting language.';
//Declare a user-defined function
function combine_string()
{
    /*$GLOBALS array is used to identify the global variable
    and assign value to the undefined global variable*/

    $GLOBALS['value'] = $GLOBALS['value1'].$GLOBALS['value2'];
    //Print the value of the global variable
    echo "<b>The value of the global variable inside the function is
:</b><br />"
. $GLOBALS['value'] ."<br/><br/>";
}
//Call the function
combine_string();
//Assign value to the undefined global variable
$value = $value1. " is a server-side language.";
//Print the value of the global variable
echo "<b>The value of the global variable outside the function is :</b><br /> $value";
?>

Output:

The following output will appear after running the above script from the server. After calling the combine_string() function, the combined value of $value1 and $value2 is printed. The value of $value1 is combined with another string and printed outside the function.

Example 4: Using a global variable in function argument

The following example shows how the global variable can be used as the function argument as a reference. Create a PHP file with the following script. Here, the $n variable is a global variable that is passed as a reference variable to the function named check(). The value of the global variable is changed inside the function and the variable is printed outside the function.

<?php
//Define global variable
$n = 10;
//Define the function
function check(&$num)
{
    //Check the number
    if($num%2 == 0){
        $string = "The number is even";
    }
    else{
        $string = "the number is odd.";
    }
    //Increment the global variable
    $num++;
    return $string;
}
//Call the function using global variable as reference
$result = check($n);
//Print the return value
echo $result. "<br/>";
//Print the global variable
echo "The value of the global variable is $n";
?>

Output:

The following output will appear after running the above script from the server. The initial value of $n is 10 that is incremented by 1 inside the function. $n is printed later.

Conclusion

The global variable is an essential part of any PHP script. No script can be written without using global variables. The uses of user-defined variables are mainly focused on this tutorial. How the global variables can be used inside the function is explained also in this tutorial by using the global keyword and $_GLOBALS[] array that is a superglobal variable.



from Linux Hint https://ift.tt/3aWPmWL

Post a Comment

0 Comments