一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

PHP 變量的作用域含代碼

PHP 變量的作用域

變量的作用域被定義為程序中可以訪問到它的范圍。愛掏網(wǎng) - it200.com換句話說,”變量的作用域是在其定義和可以訪問的程序部分內(nèi)”。愛掏網(wǎng) - it200.com

PHP有三種類型的變量作用域:

  1. 局部變量
  2. 全局變量
  3. 靜態(tài)變量

在函數(shù)內(nèi)部聲明的變量稱為該函數(shù)的局部變量。愛掏網(wǎng) - it200.com這些局部變量的作用域僅限于它們聲明的特定函數(shù)內(nèi)部。愛掏網(wǎng) - it200.com這意味著這些變量無法在函數(shù)外部訪問,因?yàn)樗鼈兙哂芯植孔饔糜颉?b class="xhide">愛掏網(wǎng) - it200.com

與函數(shù)內(nèi)部聲明的變量不同,函數(shù)外部以相同名稱聲明的變量是完全不同的。愛掏網(wǎng) - it200.com讓我們通過一個示例來理解局部變量:

文件:local_variable1.php

<?php
    function local_var()
    {
        num = 45;  //local variable
        echo "Local variable declared inside the function is: ".num;
    }
    local_var();
?>

輸出:

Local variable declared inside the function is: 45

文件:local_variable2.php

<?php
    function mytest()
    {
        lang = "PHP";
        echo "Web development language: " .lang;
    }
    mytest();
    //using lang (local variable) outside the function will generate an error
    echolang;
?>

輸出:

Web development language: PHP
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28

全局變量

全局變量是在函數(shù)外部聲明的變量。愛掏網(wǎng) - it200.com這些變量可以在程序的任何地方訪問。愛掏網(wǎng) - it200.com要在函數(shù)內(nèi)部訪問全局變量,請?jiān)谧兞壳笆褂肎LOBAL關(guān)鍵字。愛掏網(wǎng) - it200.com然而,這些變量也可以直接在函數(shù)外部訪問或使用,無需任何關(guān)鍵字。愛掏網(wǎng) - it200.com因此,無需使用任何關(guān)鍵字來訪問函數(shù)外部的全局變量。愛掏網(wǎng) - it200.com

讓我們通過一個示例來理解全局變量:

示例

文件:global_variable1.php

<?php
    name = "Sanaya Sharma";        //Global Variable
    function global_var()
    {
        globalname;
        echo "Variable inside the function: ". name;
        echo "</br>";
    }
    global_var();
    echo "Variable outside the function: ".name;
?>

輸出:

Variable inside the function: Sanaya Sharma
Variable outside the function: Sanaya Sharma

注意:如果不使用global關(guān)鍵字,在函數(shù)內(nèi)部嘗試訪問全局變量時會產(chǎn)生一個錯誤,提示變量未定義。愛掏網(wǎng) - it200.com

示例

文件:global_variable2.php

<?php
    name = "Sanaya Sharma";        //global variable
    function global_var()
    {
        echo "Variable inside the function: ".name;
        echo "</br>";
    }
    global_var();
?>

輸出:

Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6
Variable inside the function:

使用$GLOBALS而不是global

在函數(shù)內(nèi)部使用全局變量的另一種方式是預(yù)定義的$GLOBALS數(shù)組。愛掏網(wǎng) - it200.com

示例:

文件:global_variable3.php

<?php
    num1 = 5;      //global variablenum2 = 13;     //global variable
    function global_var()
    {
            sum =GLOBALS['num1'] + GLOBALS['num2'];
            echo "Sum of global variables is: " .sum;
    }
    global_var();
?>

輸出:

Sum of global variables is: 18

如果兩個變量local和global有相同的名稱,那么在函數(shù)內(nèi),局部變量比全局變量具有更高的優(yōu)先級。愛掏網(wǎng) - it200.com

示例:

文件:global_variable2.php

<?php
    x = 5;
    function mytest()
    {x = 7;
        echo "value of x: " .$x;
    }
    mytest();
?>

輸出:

Value of x: 7

注意:本地變量優(yōu)先于全局變量。愛掏網(wǎng) - it200.com

靜態(tài)變量

PHP有一個特性,即在變量執(zhí)行完畢并釋放內(nèi)存后會刪除變量。愛掏網(wǎng) - it200.com有時候,我們需要在函數(shù)執(zhí)行完畢后仍然保留變量。愛掏網(wǎng) - it200.com因此,變量作用域的另一個重要特性是靜態(tài)變量。愛掏網(wǎng) - it200.com我們在變量前面使用static關(guān)鍵字定義一個變量,這個變量被稱為 靜態(tài)變量愛掏網(wǎng) - it200.com

靜態(tài)變量只存在于局部函數(shù)中,并且在程序執(zhí)行離開作用域后不釋放其內(nèi)存。愛掏網(wǎng) - it200.com通過以下示例來理解:

示例

文件:static_variable.php

<?php
    function static_var()
    {
        static num1 = 3;       //static variablenum2 = 6;          //Non-static variable
        //increment in non-static variable
        num1++;
        //increment in static variablenum2++;
        echo "Static: " .num1 ."</br>";
        echo "Non-static: " .num2 ."</br>";
    }

//first function call
    static_var();

    //second function call
    static_var();
?>

輸出:

Static: 4
Non-static: 7
Static: 5
Non-static: 7

你必須注意,在每次函數(shù)調(diào)用后,num1會定期遞增,而num2不會。愛掏網(wǎng) - it200.com這是因?yàn)?num1不是一個靜態(tài)變量,所以它在每次函數(shù)調(diào)用后釋放了內(nèi)存。愛掏網(wǎng) - it200.com

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 午夜精品久久久久久久久久久久 | 高清黄色毛片 | 亚洲成人免费av | 四虎成人精品永久免费av九九 | 国产xxxx岁13xxxxhd | 久久久国产一区二区三区 | 在线成人av | 亚洲精品在线免费看 | 精品国产欧美一区二区 | 日本精品久久 | 自拍偷拍第一页 | 日韩中文欧美 | 国产午夜精品理论片a大结局 | 99精品电影| 精品日韩在线 | 亚洲欧美另类在线 | 国产精品久久久久久久久久三级 | 亚洲精品一区二区三区丝袜 | 黄色三级免费网站 | 奇米影视首页 | 欧美日韩精品在线免费观看 | 欧美成人a∨高清免费观看 91伊人 | 亚洲日韩中文字幕一区 | 国产精品视频免费播放 | 成人免费视频观看 | 少妇黄色 | 九九亚洲 | 久久久久久成人 | av国产精品| 国产精品成人国产乱一区 | 欧美一级视频 | 成人片网址 | 亚洲免费人成在线视频观看 | 国产成人精品一区二区三 | 欧美日韩精品区 | 欧美精品一区二区免费 | 91.com在线观看 | 精品中文在线 | 一区二区福利视频 | 国产综合久久 | 日韩精品一区二区三区中文在线 |