PHP和函数范围 [英] PHP and function scope

查看:120
本文介绍了PHP和函数范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ my_variable ='foo'; 
函数whatever(){
$ my_variable ='bar';
global $ my_variable;
}

我知道,在函数 $ my_variable 现在是'foo'。



内部发生了什么?当我在函数内执行 $ my_variable ='bar'; 时,我创建了一个局部变量。当我在下一行执行 global $ my_variable; 时会发生什么?本地的一个会自动删除? ,本地人会自动删除或者更好的措辞,全局变量。


I was wondering what exactly happens when I do this:

$my_variable = 'foo';
function whatever(){
    $my_variable = 'bar';
    global $my_variable;
}

I know that, within the scope of the function $my_variable is now 'foo'.

What's going on internally? When I do $my_variable = 'bar'; inside my function, I've created a local variable. When I do global $my_variable; on the next line what exactly happens? The local one is automatically deleted?

解决方案

Yes, the local one is automatically deleted or probably better worded, it is replaced with the global variable.

这篇关于PHP和函数范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆