Home > Backend Development > PHP Tutorial > PHP Global variable defines the global variable implementation of the current page_PHP tutorial

PHP Global variable defines the global variable implementation of the current page_PHP tutorial

WBOY
Release: 2016-07-21 15:08:19
Original
703 people have browsed it

In practical applications, PHP Global variables will find many problems that require us to constantly improve and deal with them. In this article, we give some specific solutions to problems with PHP Global variables.

1: The role of PHP Global variables is to define global variables , but this global variable is not applied to the entire website, but to the current page, including all files in include or require

Copy code The code is as follows:

$a=123;

function aa()
{
Global $a;
//If $a is not defined as a global variable
, $a cannot be accessed within the function body
echo $a;
}
aa();

Summary: PHP Global variables defined inside the function body can be used outside the function body, while global variables defined outside the function body cannot be used inside the function body.
Copy the code The code is as follows:

$glpbal $a; $a=123; function f() { echo $a; //Error, }

Look at the following example again
Copy the code The code is as follows:

function f()
{
global $a;
$a=123;
}
f();
echo $a; //Correct, you can use

2 : PHP Global variable problem analysis:
question: I defined some variables ($a) in config.inc.php, and included ("config.inc.php") outside the function in other files. These variables $a need to be used inside the function. If they are not declared, echo $a will not print anything. Therefore, global $a is declared, but there are many functions and many variables. You can't declare it like this repeatedly, right? If there is any good solution, please give me some advice.

answer1: First define constants in config.inc.php: define (constant name, constant value), then require 'config.inc.php' in other places where it needs to be used, and then you can use it here This constant is used directly in the file.
answer2: I also have a way, which is to define an array, such as $x[a], $x, so that you only need to declare global $x.
answer3: I tried your method and it didn’t work.
answer4: Change your php.ini file.
Set the PHP Global variable to on

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327470.htmlTechArticleIn practical applications, PHP Global variables will find many problems that require us to constantly improve and deal with them. In this article, we give some specific solutions to the problems that arise with PHP Global variables...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template