开发者

Global variable Scope

开发者 https://www.devze.com 2023-01-17 17:28 出处:网络
Its possible to use global variables declared in one fu开发者_运维技巧nction into an another function in php?Yes.

Its possible to use global variables declared in one fu开发者_运维技巧nction into an another function in php?


Yes.

function func_A () {
    global $var;
    $var = 5;
}

function func_B () {
    global $var;
    echo $var;
}

func_A();
func_B();

echoes 5.

What happens when you use the global keyword is, PHP makes a note that the variable name you gave refers to that variable name in the global scope (whether or not that variable exists in the global scope at that time).

Use of global functions should be avoided where possible, as they can lead to functions having side-effects.

edit: I mean "global variables", not "global functions".


Yes, see PHP Manual - Variable scope - The global keyword

0

精彩评论

暂无评论...
验证码 换一张
取 消