Variable scope of php custom function

We learned several different rules through studying the function definition part in the previous chapter:

When the function is defined, the variables in the parentheses are formal parameters ( formal parameters) and has nothing to do with variables outside the function. Just executing inside the function

Variables declared within the function have nothing to do with variables outside the function.

However, we will encounter such a situation in actual processing:

I want the variables defined in the function body to be used outside the function

I want to use the function The variables outside the body are used inside the function

At this time we need to use super global variables. Let’s review the previous knowledge points:

Global variable nameFunction description
$_COOKIEGet the value passed by cookie in session control
$_SESSIONGet the value of session in session control
$_FILESGet the result of file upload
$_GETGet the result of get passed value
$_POSTGet the value-passed result of post
$_REQUESTYou can get the value-passed result of get , you can also get the result of the value passed by Post

Let’s observe the characteristics of external variables (super global variables) through experiments and break the rules we summarized at the beginning of this chapter:

Let’s define the global.html page to write HTML content:

<html>
   <head>
       <title>超全局数组实验</title>
   </head>
   <body>
       <!--先用POST来实验,以后你可以改成GET哟 -->
       <form action="glob.php" method="post">
           <input type="text" name="hongniu" /><br />

           <input type="submit" value="提交" />
       </form>

   </body>
</html>

We submitted the html content to the glob.php page through the form form. Let’s write glob.php now:

<?php 
function demo(){ 
echo $_POST['hongniu']; 
} 
demo(); 
?>

Through this small example, you will find the super-global $ This series of superglobal variables (external variables) such as _POST can also be used inside the function. There is no restriction on variable scope as at the beginning of this article. In fact, all our declared variables are placed under the $GLOBALS array. For example:

<?php
$hello = 10;

echo $GLOBALS['hello'].'<br />';

$GLOBALS['hello'] = '我爱你';

echo $hello;

?>

Through the above example, you will find that $variable name is equivalent to $ GLOBALS['variable name'] . All variables are placed in $GLOBALS. And $GLOBALS is also global.

Therefore, we can achieve our goal: use the variables (local variables) in the function body outside the function. You can also use variables outside the function inside the function.

1. Read external variables through $GLOBALS

<?php

$one = 10;

function demo(){
   $two = 100;

   $result = $two + $GLOBALS['one'];

   return $result;

}
//你会发现结果变成了110
echo demo();

?>

In the above example: we get the variables outside the function body through $GLOBALS and use them in the function body. Therefore, the restriction that variables outside the function cannot be used within the function body is broken.

2. Modify external variables within the function through $GLOBLAS

<?php

$hongniu = '我是一个兵,来自老百姓';

function test(){

   echo '执行了函数test哟<br />';
   //调用test()函数,将通过$GLOBALS['hongniu'],把$hongniu的值改变掉

   $GLOBALS['hongniu'] = '帮助别人很快乐';
}

test();
//发现是不是输出的值变了呀?
echo $hongniu;

?>

Through the above example: we found that through $GLOBALS['hongniu'], $hongniu's The value changes, so when $hongniu is finally output, the value changes.

3. Create global variables within the function through $GLOBLAS

<?php

function hello(){

   $GLOBALS['que'] = '提神喝茶更好哟';

   echo '你调了一下函数hello<br />';
}

hello();

echo $que;

?>

In the above example, we found that $que does not exist, and we can call the function hello() Finally, echo $que can be executed outside the function, and the result can be seen: it is better to drink tea to refresh yourself. We understand that variables declared within a function can also be displayed by declaring a variable through $GLOBALS.

The following knowledge is at the [understanding] level. We use this method less and less to declare global variables. Using the global keyword followed by one or more variables within a function turns the variable into a global variable. The format is as follows:

global $variable1[, variable2,....variablen ]

Global can be followed by one or more variables, and the variables are separated by commas.

<?php
$a = 10;
$b = 100;
function test(){
   global $a , $b;
   echo $a + $b;
}
//结果是不是显示出来了?
test();
?>

In the above example, you can also try to follow the global keyword with an uncreated variable, define the variable value in the function body, and try modifying the variable value. In fact, it is the same as $GLOBALS, but the usage is different.

Note:
Do not write $variable = value after global.


global
Pronunciation: [ˈgləʊbl]
Explanation: global, global

Continuing Learning
||
<?php function hello(){ $GLOBALS['que'] = '提神喝茶更好哟'; echo '你调了一下函数hello<br />'; } hello(); echo $que; ?>
submitReset Code