php中局部变量和全局变量 代码1:函数内部使用函数外部变量错误方法 <?php $name = 'fish'; function animal() { echo $name; } animal(); 运行结果:Notice: Undefined variable: name in D:\wamp\www\index.php on line 5 由此得知,animal()方法中的name和方法外部的name和方法外部的name不是同一个变量. 如何才能在函数内部访问到函数外部的变量呢?可以在函数
发现一个问题. python中list变量作为全局变量时,在函数中可以直接修改. 而普通变量则需要先在函数中global声明,否则会报错. 例如: a = 1 def fun(): global a a = 2 而list: b = [1,2] def fun(): b[0] = 2 在函数中直接修改list则是可以的. 原因是: 普通变量如果在函数中赋值 a = 2 会有歧义.因为它既可以是表示引用全局变量a,也可以是创建一个新的局部变量,所以在python中,默认它的行为是创建局部变量,除非