search
HomeBackend DevelopmentPHP ProblemHow to use php global variables

How to use php global variables

Oct 10, 2020 pm 05:15 PM
phpglobal variables

How to use php global variables: You can declare the variable through global within the function, the code is [$name = "why";function changeName(){global $name;$name = "what"; }].

How to use php global variables

How to use php global variables:

1. Definition and use of global variables

Initially, my requirement was as follows:

<?php
$name = "why";
function changeName(){
    $name = "what";
}
changeName();
echo "my name is " . $name . "<br/>";
?>

The result of executing the code is: my name is why, instead of what is displayed after executing changeName(). Analyzing the reason, this is because the $name variable in the function body changeName is set to a local variable by default, and the scope of $name is within changeName. So, modify the code and add global variables as follows:

<?php
global $name;
$name = "why";
function changeName(){
    $name = "what";
}
changeName();
echo "my name is " . $name . "<br/>";
?>

Note: You cannot assign a value to a variable while declaring it global. The usage of global $name = "why" is wrong.

The execution result after defining the global variable is still my name is why. This result surprised me. It turns out that the correct usage of global is: "Introduce an external variable into a function. If the variable is not passed in through a parameter, then it is introduced through global." In other words, when a function refers to an external variable, it can Declare the variable via global within the function so that the variable can be used in the function (equivalent to passing it in as a parameter). Then, modify the code again:

<?php
$name = "why";
function changeName(){
    global $name;
    $name = "what";
}
changeName();
echo "my name is " . $name . "<br/>";
?>

The result of this operation is: my name is what, indicating that global is used to pass parameters, rather than making the scope of the variable global. Look at the following example again:

<?php
$name = "why";
function changeName(){
    global $name;
    $name = "what";
}
function changeName2(){
    $name = "where";
}
changeName();
changeName2();
echo "my name is " . $name . "<br/>";
?>

The execution result is: my name is what. If global $name is added to changeName2(), the execution result is: my name is where.

In summary, the role of global is equivalent to passing parameters. If you want to use a variable declared outside the function, use global to declare the variable, which is equivalent to passing the variable in. , you can reference the variable.

In addition to using the global parameter definition, you can also use the super global variable $GLOBAL:

<?php
$name = "why";
function changeName(){
    $GLOBALS[&#39;name&#39;] = "what";
}
changeName();
echo "my name is " . $name . "<br/>";
?>

2, global and $GLOBAL

An example:

<?php
$var1 = 1;
$var2 = 2;
function test1(){
    $GLOBALS[&#39;var2&#39;] = &$GLOBALS[&#39;var1&#39;];
}
test1();
echo $var2 . "<br />";
$var3 = 1;
$var4 = 2;
function test2(){
    global $var3,$var4;
    $var4 = &$var3;
}
test2();
echo $var4 . "<br />";
?>

The output value of $var2 is 1 and the value of $var4 is 2, because the reference of $var3 points to the reference address of $var4. The actual value of $var4 has not changed. The official explanation is: $GLOBALS['var'] is the external global variable itself, and global $var is the reference or pointer of the same name of the external $var. It means that $GLOBAL is available in all scopes of a script, and you can access them in functions or methods without executing global $variable;. Unlike all other superglobal variables, $GLOBALS is always available in PHP. Another example:

<?php
$var1 = 1;
function test1(){
    unset($GLOBALS[&#39;var1&#39;]);
}
test1();
echo $var1 . "<br />";
$var2 = 1;
function test2(){
    global $var2;
    unset($var2);
}
test2();
echo $var2;
?>

The output result is that $var1 does not exist, and the value of $var2 is 1. This proves that $var2 is just an alias reference, and its value has not been changed in any way. In other words, global $var is actually $var = &$GLOBALS['var'], which is just an alias for calling external variables!

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of How to use php global variables. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!