Home  >  Article  >  Backend Development  >  What should I do if php global variables cannot be used within functions?

What should I do if php global variables cannot be used within functions?

青灯夜游
青灯夜游Original
2021-10-08 18:36:423603browse

Solution: 1. Use the global keyword to import global variables in the function, the syntax is "global global variable 1, global variable 2, ..."; 2. Use the super global variable "$GLOBALS" to access the specified Global variables, the syntax is "$GLOBALS['global variable name']".

What should I do if php global variables cannot be used within functions?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Generally, global variables cannot be used within functions But sometimes we have to use global variables within a function, what should we do?

PHP provides two solutions: global keyword and $GLOBALS super global variable.

Method 1. Use the global keyword

<?php
$a = 1;
$b = 2;
$c = 3;
function demo(){
	global $a,$b,$c;
    echo &#39;$a = &#39;.$a;
    echo &#39;<br>$b = &#39;.$b;
    echo &#39;<br>$c = &#39;.$c;
}
demo();
?>

to see the output result:

$a = 1
$b = 2
$c = 3

Analysis:

The function of the global keyword is to import global variables and import global variable 1, variable 2... into a function in the form of global variable 1, variable 2, ... Within the local scope of the function, global variables defined outside the function can be used inside the function.

You need to pay attention to the following points when using the global keyword:

  • The global keyword can only be used inside the function, not outside the function;

  • The global keyword can only be used to refer to global variables outside the function, and cannot be directly assigned when referencing. The assignment and declaration statements need to be written separately;

  • In the function When a variable modified with the global keyword is destroyed internally, variables outside the function are not affected.

Method 2: Using $GLOBALS super global variable

<?php
$a = 1;
$b = 2;
$c = 3;
function demo(){
    echo &#39;$a = &#39;.$GLOBALS[&#39;a&#39;];
    echo &#39;<br>$b = &#39;.$GLOBALS[&#39;b&#39;];
    echo &#39;<br>$c = &#39;.$GLOBALS[&#39;c&#39;];
}
demo();
?>

The output result is:

$a = 1
$b = 2
$c = 3

Analysis:

$GLOBALS is a predefined variable (also called a super global variable), which is a global combined array containing all variables. The name of the variable is the key of the array. You can use $GLOBALS[ 'Variable name'] to access the specified global variable. As long as it is a global variable that has appeared, it can be obtained through the $GLOBALS array.

The difference between global and $GLOBALS

  • global can only be used inside the function, not outside the function; while $GLOBALS can be used in the program It can be used anywhere (inside and outside functions).

  • When a variable modified with the global keyword is destroyed inside a function, variables outside the function are not affected; $GLOBALS is.

    Reason:

    When using the global keyword to modify the $var variable, it is a reference to the variable of the same name outside the function, and the inside and outside are two variables that do not affect each other;

    And $GLOBALS['var'] refers to the external variable of the function itself, which is a variable.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if php global variables cannot be used within functions?. 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