php的$GLOBALS例子

來源:互聯網
上載者:User

標籤:

 1 <?php  2    $test = "test"; 3    function show1($abc){//直接把參數傳入函數,函數能用 4        echo $abc.‘<br>‘; 5    } 6     7    function show2(){//不給函數傳參數,所以使用不了外部變數,報錯 8        echo $test.‘<br>‘; 9    }10    11    function show3(){//可以通過$GLOBALS來調用外部變數12        echo $GLOBALS[‘test‘].‘<br>‘;13    }14    echo "it is show1 function<br>";15    show1($test);16    echo "it is show2 function<br>";17    show2();18    echo "it is show3 function<br>";19    show3();20    21 ?>
View Code

運行結果如下:

 

PS:補充一下$GLOBALS的知識:

       $GLOBALS :引用全域範圍中可用的全部變數

                      一個包含了全部變數的全域群組合數組。變數的名字就是數組的鍵。

  範例:

<?phpfunction test() {    $foo = "local variable";    echo ‘$foo in global scope: ‘ . $GLOBALS["foo"] . "\n";    echo ‘$foo in current scope: ‘ . $foo . "\n";}$foo = "Example content";test();?>
View Code

 

以上常式的輸出類似於:

$foo in global scope: Example content$foo in current scope: local variable
View Code

 

 

php的$GLOBALS例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.