@Chiang
2020-10-02T03:40:03.000000Z
字数 3009
阅读 809
2020-10 心得
PHP里面只有全局作用域和函数作用域,没有块作用域
- static 放在函数内部修饰变量
// 在函数执行完后,变量值仍然保存function testStatic() {static $val = 1;echo $val;$val++;}testStatic(); //output 1testStatic(); //output 2testStatic(); //output 3
- static放在类里修饰属性,或方法
// 修饰属性或方法,可以通过类名访问,如果是修饰的是类的属性,保留值class Person {static $id = 0;function __construct() {self::$id++;}static function getId() {return self::$id;}}echo Person::$id; //output 0echo "<br/>";$p1=new Person();$p2=new Person();$p3=new Person();echo Person::$id; //output 3
- static放在类的方法里修饰变量
// 修饰类的方法里面的变量class Person {static function tellAge() {static $age = 0;$age++;echo "The age is: $age";}}echo Person::tellAge(); //output 'The age is: 1'echo Person::tellAge(); //output 'The age is: 2'echo Person::tellAge(); //output 'The age is: 3'echo Person::tellAge(); //output 'The age is: 4'
- static修饰在全局作用域的变量
// 修饰全局作用域的变量,没有实际意义(存在着作用域的问题,详情查看)static $name = 1;$name++;echo $name;另外:考虑到PHP变量作用域include 'ChromePhp.php';$age=0;$age++;function test1() {static $age = 100;$age++;ChromePhp::log($age); //output 101}function test2() {static $age = 1000;$age++;ChromePhp::log($age); //output 1001}test1();test2();ChromePhp::log($age); //outpuut 1
- PHP里面只有全局作用域和函数作用域,没有块作用域
include 'ChromePhp.php';$age = 0;$age++;for ($i=0; $i<10; $i++) {$age++;}ChromePhp::log($i); //output 10;ChromePhp::log($age); //output 11;
$arr = [1, 2, 4];foreach ($arr as $val) {$sum += $val;}var_dump($val,$sum);exit;// 输出int(4)int(7)
为什么foreach 循环内的sum变量已经退出循环了,打印出来还是显示变量存在?原因是:PHP有函数作用域,但是没有块级作用域,没有C/C++、java等语言的块级作用域概念,比如C++里面:
//c++for(int i=0;i<10;i++){cout<<i<<endl;}i=11;//编译错误
因此,对于PHP以下几种情况变量都是存在的:
for($i=0;$i<10;$i++){;}var_dump($i);//输出int(10)// 不管隐藏几层循环还是一样for ($i = 0; $i < 10; $i++) {for ($j = 0; $j < 10; $j++) {$k = 777;}}var_dump($j);//输出10var_dump($k);//输出777
建议:每次使用变量前都初始化:
for ($i = 0; $i < 10; $i++) {for ($j = 0; $j < 10; $j++) {$k = 777;}}$k = 0;//初始值,重新使用
那是不是初始化就万无一失了呢?看下面这个例子
$arr = [1, 2, 4];foreach ($arr as &$val) {$val *= 2;}$val = [];//重新赋值$val[0]=9;$val[1]=10;var_dump($arr,$val);//输出结果array(3) {[0]=>int(2)[1]=>int(4)[2]=>&array(2) {[0]=>int(9)[1]=>int(10)}}array(2) {[0]=>int(9)[1]=>int(10)}
可以看到$arr数组的最后一个元素变成引用$val了。虽然重新赋值为空数组,但后面的修改还是会影响到了$arr。原因是$val是一个引用,后面的修改都会影响到,除非加unset($val);
$arr = [1, 2, 4];foreach ($arr as &$val) {$val *= 2;}unset($val);$val = [];$val[0] = 9;$val[1] = 10;var_dump($arr, $val);// 输出结果:array(3) {[0]=>int(2)[1]=>int(4)[2]=>int(8)}array(2) {[0]=>int(9)[1]=>int(10)}
- 作用域:变量能被访问到的区域 。php中的作用域与js中有所不同
- js中函数内可以直接访问到全局变量,而在php中则无法直接访问
- php变量的作用域可以分为三种
简单理解,就是函数外部定义的变量,只能在函数外部进行访问
其实就是函数内部定义的变量,只能在函数内部使用
系统定义的变量,包括的全部变量,任何地方都能访问
如何实现函数内部与函数外部的互相访问?
首先看一段代码,这样是无法运行的
$a="123";function fun(){echo $a;}fun();
- 在函数中传入参数
$a
$a="123";function fun($a){echo $a;}fun($a);
- 使用超全局变量$GLOBALS在函数内部进行访问
$a="123";function fun(){echo $GLOBALS['a'];}fun();
- 实现全局和局部的互相访问。使用Global关键字
// 全局空间已存在变量,局部访问全局$a="123";function fun(){global $a;echo $a;}fun();// 全局空间不存在这个变量,全部访问局部function fun(){global $a;$a="123";}fun();echo $a;
参考资料:
PHP之static静态变量详解
php坑系列之块级作用域
华子的程序人生
php 中use关键字的用法
匿名函数
Arrow Functions
ES5 函数
ES6+ 函数的扩展
