PHP文法_PHP教程

來源:互聯網
上載者:User
PHP 基本文法看起來很熟悉。 運行結果: Hello, World! 變數標記為“$”。你也可以將上面的“Hello, World!”寫為如下代碼: 字串的串聯則是用“.”(一個英文句點)標示出來;其他有關數位運算子號則如同你所預期的一樣: 運行結果: Hello 6 people! PHP 有完整的運算子,其功能同你所預期的一樣——特別是當你有C或者C++的知識背景時的時候。使用PHP一條很好的經驗法則是:“遇到問題,先試試看,很可能成功。” 正如在Perl一樣,一個字串用雙引號括起來,這將使得其中的變數被值置換,而如果以單引號括起來,則不會。因此,如下代碼: 運行結果: Hello, Susannah! Hello, $name! 注意字串中“ ”是分行符號號,就跟在Perl或C一樣。不過這僅在以雙引號括起來的字串內才有效。 變數 PHP 可以把環境變數當作一般變數使用。這包括由伺服器為一段CGI 程式所設定的環境變數(即使當你以模組方式執行PHP)。因此,如果頁面http://www.domain.com/farm/cattle/cow-cow.cow.html包括以下代碼: 它將輸出[/farm/cattle/cow-cow-cow.html] 數組 使用方括弧([ and ])設定數組索引(一般或關聯性): $fruit[0] = banana; $fruit[1] = papaya; $favorites[animal] = turtle; $favorites[monster] = cookie; 如果你對數組賦值,但索引是空白的,PHP則會把對象放於數組末尾。上面對變數$fruit的聲明同下面代碼的結果是一樣的: $fruit[] = banana; $fruit[] = papaya; 你也可以用多維陣列: $people[David][shirt] = blue; $people[David][car] = minivan; $people[Adam][shirt] = white; $people[Adam][car] = sedan; 一個建立數組的簡便方法是 array() 函數為: $fruit = array(banana,papaya); $favorites = array(animal => turtle, monster => cookie); 或者 $people = array (David => array(shirt => blue, car => minivan), Adam => array(shirt => white, car => sedan)); 內建函數count()表明一個數組裡有多少元素: $fruit = array(banana,papaya); print count($fruit); 得到如下結果 2 控制結構 你可以利用迴圈結構例如for以及while: for ($i = 4; $i < 8; $i++) { print "I have eaten $i bagels today."; } 結果 I have eaten 4 bagels today. I have eaten 5 bagels today. I have eaten 6 bagels today. I have eaten 7 bagels today. 同樣可寫為 $i = 4; while ($i < 8) { print "I have eaten $i bagels today."; $i++; } 你可以使用控制結構if以及elseif: if ($user_count > 200) { print "The site is busy right now!"; } elseif ($user_count > 100) { print "The site is sort of active right now!"; else { print "The site is lonely - only $user_count user logged on."; } 使用運算子的經驗法則同樣也可以運用在控制結構上面。你還可以使用switch,do...while,甚至是 ?: 結構。

http://www.bkjia.com/PHPjc/531893.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/531893.htmlTechArticlePHP 基本文法看起來很熟悉。 運行結果: Hello, World! 變數標記為“$”。你也可以將上面的“Hello, World!”寫為如下代碼: 字串的串聯則是...

  • 聯繫我們

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