您的位置:首页 > 编程语言 > PHP开发

读书笔记:php_tizag_tutorial

2015-07-11 12:31 711 查看
  昨天在实验室花了一天时间看了英文版的php_tizag_tutorial,因为上学期用php和bootstrap写过一个租房网站,对php还是比较熟悉.现在总结一下php_tizag_tutorial里面一些没用过或者遗忘的知识点.

1.打印的字符串里面包含引号时,要注意采取下面的两种方式之一:
1). 在引号前加上反斜杠"\".
2). 使用单引号.
举例如下:

<?php
// This won't work because of the quotes around specialH5!
echo "<h5 class="specialH5">I love using PHP!</h5>";
// OK because we escaped the quotes!
echo "<h5 class=\"specialH5\">I love using PHP!</h5>";
// OK because we used an apostrophe '
echo "<h5 class='specialH5'>I love using PHP!</h5>";
?>


2.String Creation Heredoc
  使用<<<和自定义的一些标识符(这里用的是TEST)开始字符串的heredoc,最后以"标识符;"作为结束标志.
注意标识符要在单独一行,不可缩进.
举例如下:

$my_string = <<<TEST
Tizag.com
Webmaster Tutorials
Unlock your potential!
TEST;
echo $my_string;


输出结果:

Tizag.com Webmaster Tutorials Unlock your potential!


3.include函数与require函数的区别
  使用include函数时,若出现错误,只是提出警告,代码依然执行下去;使用require函数时,则会报错,并且终止执行下去.
强烈推荐使用后者.

4.POST与GET的区别
1). POST
HTML代码片段:

<form action="process.php" method="post">
<select name="item">
...
<input name="quantity" type="text" />


PHP代码片段:

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


2). GET

HTML代码片段:

<form action="process.php" method="get">
<select name="item">
...
<input name="quantity" type="text" />


PHP代码片段:

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


3). 两者区别

  GET传递数据是通过url,上面的例子中跳转到process.php页面是,会在url后面附上"?item=##&quantity=##",其中?是告诉浏览器接下来的这几项是变量.所以,传递重要信息如密码等,就不要使用GET方式传递数据,这样会完全暴露出来,非常不安全.

5.PHP创建文件
  在PHP中,fopen函数用于打开一个文件,当该文件不存在时,就创建该文件.

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


6.PHP打开文件
  打开文件的方式有几种:
1). Read: 'r'
只读,文件指针在文件开头.
2). Write: 'w'
只写,文件指针在文件开头.而且会把之前的内容覆盖.
3). Append: 'a'
只写,文件指针在文件末尾.

7.PHP删除文件
  使用unlink函数.

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


8.PHP在字符串里匹配字段
  使用strpos函数返回相应字段位置.
  举例如下:

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


  结果如下:

The position of 5 in our string was 4

Finding All Occurrences in a String with Offset


  举例如下:

$numberedString = "1234567890123456789012345678901234567890";
$offset = 0; // initial offset is 0
$fiveCounter = 0;
while($offset = strpos($numberedString, "5", $offset + 1)){
$fiveCounter++;
echo "<br />Five #$fiveCounter is at position - $offset";
}


  结果如下:

Five #1 is at position - 4
Five #2 is at position - 14
Five #3 is at position - 24
Five #4 is at position - 34


9.PHP分割字符串
  使用explode函数.
  举例如下:

$rawPhoneNumber = "800-555-5555";
$phoneChunks = explode("-", $rawPhoneNumber);
echo "Raw Phone Number = $rawPhoneNumber <br />";
echo "First chunk = $phoneChunks[0]<br />";
echo "Second chunk = $phoneChunks[1]<br />";
echo "Third Chunk chunk = $phoneChunks[2]";


  结果如下:

Raw Phone Number = 800-555-5555
First chunk = 800
Second chunk = 555
Third Chunk chunk = 5555


10.PHP连接字符串
  使用implode函数.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: