讀書筆記:php_tizag_tutorial

來源:互聯網
上載者:User
  昨天在實驗室花了一天時間看了英文版的php_tizag_tutorial,因為上學期用php和bootstrap寫過一個租房網站,對php還是比較熟悉.現在總結一下php_tizag_tutorial裡面一些沒用過或者遺忘的知識點.


1.列印的字串裡麵包含引號時,要注意採取下面的兩種方式之一:
1). 在引號前加上反斜線"\".
2). 使用單引號.
舉例如下:

1 I love using PHP!";4 // OK because we escaped the quotes!5 echo "
I love using PHP!
";6 // OK because we used an apostrophe '7 echo "
I love using PHP!
";8 ?>

2.String Creation Heredoc
  使用<<<和自訂的一些標識符(這裡用的是TEST)開始字串的heredoc,最後以"標識符;"作為結束標誌.
注意標識符要在單獨一行,不可縮排.
舉例如下:

1 $my_string = <<   

輸出結果:

1 Tizag.com Webmaster Tutorials Unlock your potential!

3.include函數與require函數的區別
  使用include函數時,若出現錯誤,只是提出警告,代碼依然執行下去;使用require函數時,則會報錯,並且終止執行下去.
強烈推薦使用後者.

4.POST與GET的區別
1). POST
HTML程式碼片段:

1 2 3 ...4 

PHP程式碼片段:

1 $quantity = $_POST['quantity'];2 $item = $_POST['item'];

2). GET

HTML程式碼片段:

1 2 3 ...4 

PHP程式碼片段:

1 $quantity = $_GET['quantity'];2 $item = $_GET['item'];

3). 兩者區別

  GET傳遞資料是通過url,上面的例子中跳轉到process.php頁面是,會在url後面附上"?item=##&quantity=##",其中?是告訴瀏覽器接下來的這幾項是變數.所以,傳遞重要訊息如密碼等,就不要使用GET方式傳遞資料,這樣會完全暴露出來,非常不安全.

5.PHP建立檔案
  在PHP中,fopen函數用於開啟一個檔案,當該檔案不存在時,就建立該檔案.

1 $ourFileName = "testFile.txt";2 $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");3 fclose($ourFileHandle);

6.PHP開啟檔案
  開啟檔案的方式有幾種:
1). Read: 'r'
唯讀,檔案指標在檔案開頭.
2). Write: 'w'
唯寫,檔案指標在檔案開頭.而且會把之前的內容覆蓋.
3). Append: 'a'
唯寫,檔案指標在檔案末尾.

7.PHP刪除檔案
  使用unlink函數.

1 $myFile = "testFile.txt";2 unlink($myFile);

8.PHP在字串裡匹配欄位
  使用strpos函數返回相應欄位位置.
  舉例如下:

1 $numberedString = "1234567890"; // 10 numbers from 1 to 02 $fivePos = strpos($numberedString, "5");3 echo "The position of 5 in our string was $fivePos";

  結果如下:

1 The position of 5 in our string was 42 3 Finding All Occurrences in a String with Offset

  舉例如下:

1 $numberedString = "1234567890123456789012345678901234567890";2 $offset = 0; // initial offset is 03 $fiveCounter = 0;4 while($offset = strpos($numberedString, "5", $offset + 1)){5 $fiveCounter++;6 echo "
Five #$fiveCounter is at position - $offset";7 }

  結果如下:

1 Five #1 is at position - 4 2 Five #2 is at position - 143 Five #3 is at position - 244 Five #4 is at position - 34

9.PHP分割字串
  使用explode函數.
  舉例如下:

1 $rawPhoneNumber = "800-555-5555";2 $phoneChunks = explode("-", $rawPhoneNumber);3 echo "Raw Phone Number = $rawPhoneNumber 
";4 echo "First chunk = $phoneChunks[0]
";5 echo "Second chunk = $phoneChunks[1]
";6 echo "Third Chunk chunk = $phoneChunks[2]";

  結果如下:

1 Raw Phone Number = 800-555-55552 First chunk = 8003 Second chunk = 5554 Third Chunk chunk = 5555

10.PHP連接字串
  使用implode函數.

  • 聯繫我們

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