Home  >  Article  >  Backend Development  >  Detailed explanation of the problem of invalid php global variables_PHP tutorial

Detailed explanation of the problem of invalid php global variables_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:501039browse

The variables defined by global are global variables. For example, if the variable in the function and the variable with the same name outside the function are not the same variable in PHP, if we use global to define the variable name in the function with the same name externally, , then they are a variable. Let’s take a few examples to illustrate.

For PHP beginners, when using the global keyword, you may find that globalizing a variable outside a function within a function cannot correctly output the variable in some cases (that is, the global variable is invalid). Let's look at a simple and common example.

Here, we have two pages a.php and b.php.

b.php page code is as follows:

The code is as follows Copy code
 代码如下 复制代码

    $site_name = 'CodePlayer';

function sayHi(){
global $site_name;
echo "Hello! Welcome to $site_name !";
}
?>

$site_name = 'CodePlayer';

function sayHi(){
         global $site_name;
echo "Hello! Welcome to $site_name !";
}
?>
 代码如下 复制代码

   

function include_view_page(){
include 'b.php';
sayHi();
}

include_view_page();
?>

a.php page code is as follows:

The code is as follows Copy code

function include_view_page(){
           include 'b.php';
sayHi(); }

include_view_page();
?>

The above example is very simple. We hope that when we visit the a.php page, the welcome statement can be displayed correctly. However, unfortunately, when we use the browser to access the a.php page, we find the following output:

 代码如下 复制代码

    //b.php
    global $site_name;
$site_name = 'CodePlayer';

function sayHi(){
global $site_name;
echo "Hello! Welcome to $site_name !";
}
?>

Hello! Welcome to ! In other words, when we call the sayHi() function in the function include_view_page(), the global $site_name in the sayHi() function of the b.php page is not correctly recognized and takes effect. What is going on? In fact, when we include the b.php page in the function include_view_page(), the variable $site_name of the b.php page is equivalent to being stored in the scope within the include_view_page() function. As we all know, globalizing a variable within a function actually establishes a reference to the global variable of the page within the function. In our example, the $site_name variable is only a local variable within the include_view_page() function for a.php, so the variable cannot be globalized. Naturally, we cannot obtain the correct variables and variable values ​​when we make related calls. . In PHP, we especially need to pay attention to the above-mentioned problem of including a page within a function, causing the scope of the variables in the page to change. In order to avoid this situation, we should try to reduce multi-level include calls and try not to use include within functions. In addition, we can also declare $site_name as a global variable in the b.php page.
The code is as follows Copy code
//b.php
global $site_name;
$site_name = 'CodePlayer';<🎜> <🎜> function sayHi(){
global $site_name;
echo "Hello! Welcome to $site_name !";
}
?>

Example, referencing global variables within a function


First look at the following code:

} else { $GLOBALS["var2"] =&$var1; //2
The code is as follows
 代码如下 复制代码

$var1 = "#####";
$var2 = "&&&&&";

function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =&$var1; //1

} else {
$GLOBALS["var2"] =&$var1; //2

}
}

global_references(false);
echo "var2 is set to '$var2'
";
global_references(true);
echo "var2 is set to '$var2'
";
?>

Copy code

$var1 = "#####";

$var2 = "&&&&&";


function global_references($use_globals)
{
global $var1, $var2;
if (!$use_globals) {
$var2 =&$var1; //1

}

}

echo "var2 is set to '$var2'
"; global_references(true); echo "var2 is set to '$var2'
"; ?> The output results are as follows: var2 is set to '&&&&&'
var2 is set to '#####' As you can see, in the above code:
$var2 =&$var1; //1
Only visible inside the function. And $GLOBALS["var2"] =&$var1; //2 Visible globally. http://www.bkjia.com/PHPjc/632616.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632616.htmlTechArticleThe variables defined by global are global variables. For example, if the variables in the function have the same name as the variables outside the function It is not a variable in php. If we use global to define it in a function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn