昨天在實驗室花了一天時間看了英文版的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函數.