Invalid phpglobal variables _ PHP Tutorial

Source: Internet
Author: User
The phpglobal variable is invalid. The global variable is a global variable. for example, if the variable in the function is not a variable with the same name as the external variable in the function, if we use global to define global variables as global variables in the function, let's say, if the variables in the function are not a variable with the same name as the external function variables in php, if we use global to define the same variable name as the external variable name in the function, they are a variable. let's take a few examples to illustrate it.

For beginners of PHP, when using the global keyword, you may find that a variable outside the function is global in the function, in some cases, this variable cannot be correctly output (that is, the global variable is invalid ). The following is a simple and common example.

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

B. php page code:

The code is as follows:

$ Site_name = 'codeplayer ';

Function sayHi (){
Global $ site_name;
Echo "Hello! Welcome to $ site_name! ";
}
?>

A. php page code is as follows:

The code is as follows:

Function include_view_page (){
Include 'B. php ';
SayHi ();
}

Include_view_page ();
?>

The above example is very simple. we hope that when we access the. php page, the welcome statements will be displayed correctly. However, when we access the. php page using a browser, we find that the output result is as follows:

Hello! Welcome!

That is to say, when we call the sayHi () function in the include_view_page () function, the $ site_name of the global function in the sayHi () function on the B. php page is not correctly identified and takes effect. What exactly is this?

In fact, when we include the B. php page in the include_view_page () function, the variable $ site_name on the B. php page is stored in the scope of the include_view_page () function. As we all know, in a function, a global variable is actually a reference to the global variables on the page. In our example, the $ site_name variable is. for php, it is only a local variable in the include_view_page () function. Therefore, the global variable cannot be obtained. we naturally cannot obtain the correct variables and variable values when performing related calls.

In php, we need to pay special attention to the issue of include a page in the function, which leads to the change of the variable scope in the page. To avoid this situation, we should minimize the number of multi-level include calls and avoid using include in functions. In addition, we can declare $ site_name in the form of global variables on the B. php page.

The code is as follows:

// B. php
Global $ site_name;
$ Site_name = 'codeplayer ';

Function sayHi (){
Global $ site_name;
Echo "Hello! Welcome to $ site_name! ";
}
?>

Example: reference global variables in a function


Let's take a look at the following code:

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'
";
?>

The output result is as follows:

Var2 is set '&&&&&'
Var2 is set '#####'

As shown in the above code:
$ Var2 = & $ var1; // 1
Only visible inside the function.
While
$ GLOBALS ["var2"] = & $ var1; // 2
Visible in a global range.

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.