Get the App
SLTechnology News&Howtos  ›  Development  › 

The PHP Global variable defines what the global variable of the current page is.

Shulou Source: shulou.com Published: 2022-06-03 04:24:06 10月01日 Update

This article will explain in detail about PHP Global variable definition of the current page of the global variable is what, the content of the article is of high quality, so the editor to share with you to do a reference, I hope you have a certain understanding of the relevant knowledge after reading this article.

PHP Global variable in the practical application will find many problems that we need to constantly improve and deal with. In this article, we give some specific solutions to the problems with PHP Global variables.

The function of the 1:PHP Global variable is to define a global variable, but this global variable is not applied to the entire site, but to all files on the current page, including include or require

$axi123

Function aa ()

{

Global $a

/ / if you do not define $an as the global variable

The body of the function cannot access $a

Echo $a

}

Aa ()

Summary: the PHP Global variable defined in the function body can be used outside the function body, while the global variable defined outside the function body cannot be used in the function body.

$glpbal $a; $aq123; function f () {echo $a; / / error,}

Take a look at the following example

Function f () {global $a; $aq123;} f (); echo $a; / / correct, you can use

Resolution of 2:PHP Global variable problem:

Question: I have defined some variables ($a) in config.inc.php, external include ("config.inc.php") in other files, need to use these variables $an inside the function, and echo $a can't print anything if it's not declared. So declare global $a, but there are a lot of functions and a lot of variables, and you can't declare it over and over again, right? If there is any good solution, please give me some advice.

Answer1: first define the constant in config.inc.php: define (constant name, constant value), then require 'config.inc.php', where you need it, and then you can use the constant directly in this file.

Answer2: I also have an idea, which is to define an array, such as $x [a], $x, then just declare one global $x.

Answer3: I tried your method. I can't do it.

Answer4: change your php.ini file.

Set the PHP Global variable to on

On the PHP Global variable definition of the current page of the global variable is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Variables functions global constants files pages methods articles questions applications constantly content articles more knowledge no good things functions places Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn OPPO Reno Linux Shulou Information