Home  >  Article  >  Backend Development  >  How to make variables global variables in php

How to make variables global variables in php

silencement
silencementOriginal
2019-09-26 09:34:202806browse

How to make variables global variables in php

We know that variables are actually equivalent to the containers we use to store information. Regarding its naming rules, I believe everyone is relatively clear about it, so I won’t go into details here. Here, we mainly look at the two scopes of PHP variables, global variables & local variables.

Seeing these two variable scopes, I believe the two words global&static will come to everyone's mind. Yes, those are the two words.

We know that variables defined outside all functions have global scope. In addition to functions, global variables can be accessed by any part of the script. To access a global variable in a function, you need to use the global keyword. However, variables declared inside a PHP function are local variables and can only be accessed inside the function. Next, let's look at an example:

<?php
$x=5; // 全局变量

function myTest()
{
    $y=10; // 局部变量
    echo "<p>测试函数内变量:<p>";
    echo "变量 x 为: $x";
    echo "<br>";
    echo "变量 y 为: $y";
}

myTest();

echo "<p>测试函数外变量:<p>";
echo "变量 x 为: $x";
echo "<br>";
echo "变量 y 为: $y";
?>

In the above example, the myTest() function defines the $x and $y variables. The $x variable is declared outside the function, so it is a global variable, and the $y variable is declared inside the function, so it is a local variable.

When we call the myTest() function and output the values ​​of two variables, the function will output the value of the local variable $y, but cannot output the value of $x, because the $x variable is defined outside the function and cannot Used within a function, if you want to access a global variable in a function, you need to use the global keyword.

Then we output the values ​​of the two variables outside the myTest() function. The function will output the value of all local variables $x, but cannot output the value of $y because the $y variable is in the function. Defined in, it is a local variable.

From this, we can know that we can use the same variable name in different functions, because the variable names defined in these functions are local variables and only act within that function. This can be considered a little trick.

Without further ado, let’s look at the global keyword first. The global keyword is used to access global variables within a function. To call global variables defined outside the function within a function, we need to add the global keyword before the variables in the function. Let’s look at an example:

<?php
$x=5;
$y=10;
function myTest()
{
    global $x,$y;
    $y=$x+$y;
}
myTest();
echo $y; // 输出 15
?>

PHP stores all global variables in a file named In the array of $GLOBALS[index]. index holds the name of the variable. This array can be accessed inside the function or used directly to update global variables. Therefore, the above example can be written like this:

<?php
$x=5;
$y=10;

function myTest()
{
    $GLOBALS[&#39;y&#39;]=$GLOBALS[&#39;x&#39;]+$GLOBALS[&#39;y&#39;];
}

myTest();
echo $y;
?>

The above is the detailed content of How to make variables global variables in php. 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
Previous article:How to open php filesNext article:How to open php files