Blogger Information
Blog 28
fans 0
comment 1
visits 21452
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示变量作用域
南宫
Original
791 people have browsed it

函数会创建一个独立作用域,不能直接访问外部的变量

  1. $site = 1;
  2. function getInfo(){
  3. //为了访问外部的变量,在函数中做一个声明
  4. global $site;
  5. return $site;
  6. }
  7. echo getInfo();

超全局变量, 不受作用域限制,可以在函数中直接访问
$GLOBALS — 引用全局作用域中可用的全部变量
一个包含了全部变量的全局组合数组。变量的名字就是数组的键。

  1. $site = 1;
  2. echo $GLOBALS['site'];
  3. function getInfo(){
  4. return $GLOBALS['site'];
  5. }
  6. echo getInfo();

函数体内部变量只能函数内部访问

  1. function getInfo(){
  2. $site = 1;
  3. return $site;
  4. }
  5. //这里是不能访问的
  6. echo $site;
Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:请认真对待作业!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post