PHP5學習筆記-變數

來源:互聯網
上載者:User

PHP的變數名以貨幣符號$開始, 可以包含數字,字母,以及底線, 只能字母或者底線開頭,不能以數字開頭.

$str = "hey, 媽媽叫你回去7飯飯拉";echo $str;

還有一種是變數間的賦值

1) 一般賦值, 兩個變數享有不同記憶體地區, 改變原變數不會影響第二個變數

$num1 = 1;$num2 = $num1;$num1= 3;echo $num1 .'<br/>';echo $num2 .'<br/>';

2) 傳址/引用賦值

從PHP4.0開始引入了傳址/引用賦值, 在原變數前加 &符號, 表示對原變數的引用, 原變數改變,引用的變數的值也會相應改變

$num1 = 1;$num2 = &$num1;$num1= 3;echo $num1 .'<br/>';echo $num2 .'<br/>';

變數的範圍

分全域, 局部, 靜態變數

1)全域變數

定義在函數外部的變數,範圍是整個PHP檔案

在函數內部要使用全域變數,方式有兩種:

 一種是在函數內,對要使用的全域變數使用global關鍵字進行聲明,然後再使用就可以了;

第二種方式是直接使用內建函數$GLOBALS

DEMO:

     $name_str = "媽媽叫你回家吃飯啊,麥兜";            function getStr(){         /**方法一        //必須先聲明其為全域變數        global $name_str;        //使用全域變數        $tempt_str = 'Hello '.$name_str;          *           */         //方法二,使用內建函數$GLOBALS         $tempt_str = 'Hello '.$GLOBALS['name_str'];        return $tempt_str;    }              echo getStr();

2. 局部變數,定義在函數內部, 外部是無法使用的

3. 靜態變數,使用static聲明, 不同於普通變數, 使用後就銷毀, 靜態變數的值會一直存在於記憶體中. 進入該變數範圍後會繼續最後一次使用的值

DEMO:

 //-----------------使用靜態變數----------------------------    function momCallU(){        static $count = 1;        $count += 1;        return "Mom has called u $count time(s) for dinner, Maidou<br/>";    }    echo "-----------------使用靜態變數----------------------------<br/>";    for($i = 0; $i<10; $i++)        echo momCallU();    echo '--------------------------------------------------------<br/>';    //-------------------------使用普通變數-----------------------     function momCallU2(){        $count2 = 1;        $count2 += 1;        return "Mom has called u $count2 time(s) for dinner, Maidou<br/>";    }    echo "-----------------使用普通變數----------------------------<br/>";    for($j = 0; $j<10; $j++)        echo momCallU2();

輸出為:

-----------------使用靜態變數----------------------------Mom has called u 2 time(s) for dinner, MaidouMom has called u 3 time(s) for dinner, MaidouMom has called u 4 time(s) for dinner, MaidouMom has called u 5 time(s) for dinner, MaidouMom has called u 6 time(s) for dinner, MaidouMom has called u 7 time(s) for dinner, MaidouMom has called u 8 time(s) for dinner, MaidouMom has called u 9 time(s) for dinner, MaidouMom has called u 10 time(s) for dinner, MaidouMom has called u 11 time(s) for dinner, Maidou-------------------------------------------------------------------------使用普通變數----------------------------Mom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, MaidouMom has called u 2 time(s) for dinner, Maidou

4.可變變數

這是一種比較特殊的用法, 變數名是動態,而不是預先設定好

//-----------可變變數---------------------        $changeable = "saysth";    $saysth = "hello, Maidou!";        echo $changeable . '<br/>';        echo $$changeable . '<br/>';

輸出為:

saysthhello, Maidou!

5.預定義變數

---------------------------------------------------------------------------------------------------------------------------------------------------

變數名                                                                  說明

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['SERVER_ADDR']                    當前運行指令碼所在伺服器的IP地址

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['SERVER_NAME']                   ....的主機名稱

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['SERVER_PORT']                 ....的連接埠

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['REQUEST_METHOD']       訪問頁面時的要求方法, 如GET, HEAD, POST, PUT

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['REMOTE_ADDR']              當前瀏覽的使用者的IP

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['REMOTE_HOST']              ....的主機名稱

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['REMOTE_PORT']             ....的連接埠

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['SCRIPT_FILENAME']     當前指令碼的絕對路徑

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SERVER['DOCUMENT_ROOT']     指令碼所在文檔的根目錄,在伺服器設定檔中定義

---------------------------------------------------------------------------------------------------------------------------------------------------

$_COOKIE                                             操作COOKIE

---------------------------------------------------------------------------------------------------------------------------------------------------

$_SESSION                                          操作SESSION

---------------------------------------------------------------------------------------------------------------------------------------------------

$_GET                                                   操作GET傳遞的資訊

---------------------------------------------------------------------------------------------------------------------------------------------------

$_POST                                                操作POST傳遞的資訊

---------------------------------------------------------------------------------------------------------------------------------------------------

$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.