Blogger Information
Blog 9
fans 0
comment 0
visits 9526
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
变量容易进的“坑”
廖磊的博客
Original
806 people have browsed it


一、传值赋值与引用赋值

<?php
   $a="xiehao";
   $b=$a;  //传值赋值:将$a的值copy到$b的内存中,$a的值不因$b的值改变而改变
   $c=
&$a;  //引用赋值:$c指向$a的内存,并不会开辟新的内存  $a会根据$c的值改变而改变
   unset($a);  //unset取消引用 $c的值不变
?>

二、作用域

<?php
    $a=0;
    $b=1;
    $c=2;
    function fun(){
        echo $a;  //$a没定义
        global $b;  //$b=1
        global['$c']; //$c=2
    }
?>

三、静态变量

<?php
    function fun(){
        static $a=1;
        global $b=2;
        $a++;
        $b++;
        echo $a;
        echo $b;
    }
    for ($i=0;$i<10;$i++){
        fun();
    }
?>

输出结果:(静态变量)$a:2 3 4 5 6 7 8 9  10

$b: 3 3 3 3 3 3 3  3 3 3

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