php基礎02:變數

來源:互聯網
上載者:User

標籤:下標   class   oba   div   nbsp   訪問   函數   執行   全域   

1.建立變數
<?php    $num1 = 15;    $num2 = 15.5;    echo $num1+$num2;    echo "<br>";?>
 2.Local 和 Global 範圍
<?php    $x = 5;//Global    function myTest(){        $y = 10;//Local        echo "x:$x";        echo "<br>";        echo "y:$y";        echo "<br>";    }    myTest();    echo "<hr>";    echo "x:$x";    echo "<br>";    echo "y:$y";?>
 3.PHP global 關鍵詞
<?php    $c = 10;    $d = 20;    function test(){        global $c, $d, $f;        $f = $c + $d;    }    test();    echo "$f";?>
 4.PHP 同時在名為 $GLOBALS[index] 的數組中儲存了所有的全域變數。下標存有變數名。這個數組在函數內也可以訪問,並能夠用於直接更新全域變數。
<?php    $x = 5;    $y = 15;    function test4(){        $GLOBALS[‘y‘] = $GLOBALS[‘y‘] + $GLOBALS[‘x‘];    }    test4();    echo "$y";?>
 5.PHP static 關鍵詞

  通常,當函數完成/執行後,會刪除所有變數。不過,有時我需要不刪除某個局部變數。實現這一點需要更進一步的工作。要完成這一點,請在您首次聲明變數時使用 static 關鍵詞:

<?php    function myTest() {          static $x=0;          echo $x;          $x++;    }    myTest();    myTest();    myTest();?>

 

php基礎02:變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.