Rumah >php教程 >php手册 >PHP_Bibel阅读学习(一)看书看经典,写文写代码 - 韧还

PHP_Bibel阅读学习(一)看书看经典,写文写代码 - 韧还

WBOY
WBOYasal
2016-05-20 11:39:251002semak imbas

  基础快速再看一下,然后每天有新的好玩的看。

这本书,反正好评不少,就是`PHP和MySQL Web开发`,机械工业出版社,澳洲人写的,红皮,有兴趣的可以看一下。

 

第一篇 使用PHP

一、入门

5分钟翻一下,两个点吧。

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">//</span>
<span style="color: #008080;"> 3</span> <span style="color: #008000;">//From Bible
</span><span style="color: #008080;"> 4</span> <span style="color: #008000;">//Session1 Based
</span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//
</span><span style="color: #008080;"> 6</span> <span style="color: #008000;">//
</span><span style="color: #008080;"> 7</span> 
<span style="color: #008080;"> 8</span> <span style="color: #008000;">//Chapter1-Use</span>
<span style="color: #008080;"> 9</span> <span style="color: #000000;">namespace Bible\Basic\Using;
</span><span style="color: #008080;">10</span> 
<span style="color: #008080;">11</span> <span style="color: #008000;">//</span><span style="color: #008000;">引用操作符
</span><span style="color: #008080;">12</span> <span style="color: #008000;">//这样子赋值的变量享有同一个空间</span>
<span style="color: #008080;">13</span> <span style="color: #800080;">$a</span>=3<span style="color: #000000;">;
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>;<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #008080;">15</span> <span style="color: #800080;">$b</span>=&<span style="color: #800080;">$a</span><span style="color: #000000;">;
</span><span style="color: #008080;">16</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$b</span>;<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #008080;">17</span> <span style="color: #800080;">$b</span>=3333<span style="color: #000000;">;
</span><span style="color: #008080;">18</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>;<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #008080;">19</span> 
<span style="color: #008080;">20</span> <span style="color: #008000;">//</span><span style="color: #008000;">执行操作符
</span><span style="color: #008080;">21</span> <span style="color: #008000;">//``</span>
<span style="color: #008080;">22</span> <span style="color: #800080;">$out</span>=`ls -<span style="color: #000000;">la`;
</span><span style="color: #008080;">23</span> <span style="color: #0000ff;">echo</span> '<pre class="brush:php;toolbar:false">'.<span style="color: #800080;">$out</span>.'
';

 二、数据的存储与检索(文件直接操作

想说的都在代码里了。

<span style="color: #008080;"> 1</span> <span style="color: #000000;">namespace Bible\Basic\DataUsing;
</span><span style="color: #008080;"> 2</span> <span style="color: #008000;">//</span><span style="color: #008000;">判断文件是否存在——打开文件——锁定文件——读取文件——写入数据——解锁——关闭文件</span>
<span style="color: #008080;"> 3</span> <span style="color: #800080;">$filename</span>='test.txt'<span style="color: #000000;">;
</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">if</span> (!<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">)) {
</span><span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">echo</span> 'The file you want to visit is not exist!';<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">exit</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 8</span> <span style="color: #800080;">$fp</span>=@<span style="color: #008080;">fopen</span>(<span style="color: #800080;">$filename</span>,'r+'<span style="color: #000000;">);
</span><span style="color: #008080;"> 9</span> <span style="color: #008080;">flock</span>(<span style="color: #800080;">$fp</span>, LOCK_EX);  <span style="color: #008000;">//</span><span style="color: #008000;">锁定文件,只允许一个写入</span>
<span style="color: #008080;">10</span> <span style="color: #0000ff;">while</span> (!<span style="color: #008080;">feof</span>(<span style="color: #800080;">$fp</span>)){   <span style="color: #008000;">//</span><span style="color: #008000;">按行读取文件内容</span>
<span style="color: #008080;">11</span>     <span style="color: #800080;">$content</span>=<span style="color: #008080;">fgets</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">);
</span><span style="color: #008080;">12</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$content</span>;<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #008080;">13</span> <span style="color: #000000;">}
</span><span style="color: #008080;">14</span> <span style="color: #800080;">$writeContent</span>='I have something to write into'<span style="color: #000000;">;
</span><span style="color: #008080;">15</span> <span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>, <span style="color: #800080;">$writeContent</span>,<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$writeContent</span>));  <span style="color: #008000;">//</span><span style="color: #008000;">控制写入长度,兼容性考虑</span>
<span style="color: #008080;">16</span> <span style="color: #008080;">flock</span>(<span style="color: #800080;">$fp</span>, LOCK_UN);  <span style="color: #008000;">//</span><span style="color: #008000;">释放锁定</span>
<span style="color: #008080;">17</span> <span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span>);

 

三、数组

其他的都老生常谈,大概查一下随便用,就是熟练度的问题,但还是得知道哈。

然后学到了usort(用户自定义排序)的妙处:

<span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;">Chapter3.Array</span>
<span style="color: #008080;"> 2</span> <span style="color: #000000;">namespace Bible\Basic\ArrayUsort;
</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;">多维数组的排序</span>
<span style="color: #008080;"> 4</span> <span style="color: #800080;">$array1</span>=<span style="color: #0000ff;">array</span>(54654,545,668<span style="color: #000000;">);
</span><span style="color: #008080;"> 5</span> <span style="color: #800080;">$array2</span>=<span style="color: #0000ff;">array</span>(5454,88,37,54<span style="color: #000000;">);
</span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$array3</span>=<span style="color: #0000ff;">array</span>(123,4444,5453<span style="color: #000000;">);
</span><span style="color: #008080;"> 7</span> <span style="color: #800080;">$arrayAll</span>=<span style="color: #0000ff;">array</span>(<span style="color: #800080;">$array1</span>,<span style="color: #800080;">$array2</span>,<span style="color: #800080;">$array3</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 8</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$arrayAll</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 9</span> 
<span style="color: #008080;">10</span> <span style="color: #0000ff;">function</span> dimensionsCompare(<span style="color: #800080;">$x</span>,<span style="color: #800080;">$y</span><span style="color: #000000;">)
</span><span style="color: #008080;">11</span> <span style="color: #000000;">{
</span><span style="color: #008080;">12</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$x</span>[1]==<span style="color: #800080;">$y</span>[1<span style="color: #000000;">]){
</span><span style="color: #008080;">13</span>         <span style="color: #0000ff;">return</span> 0<span style="color: #000000;">;
</span><span style="color: #008080;">14</span>     }<span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$x</span>[1]$y[1<span style="color: #000000;">]){
</span><span style="color: #008080;">15</span>         <span style="color: #0000ff;">return</span> -1<span style="color: #000000;">;
</span><span style="color: #008080;">16</span>     }<span style="color: #0000ff;">else</span><span style="color: #000000;">{
</span><span style="color: #008080;">17</span>         <span style="color: #0000ff;">return</span> 1<span style="color: #000000;">;
</span><span style="color: #008080;">18</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">19</span> <span style="color: #000000;">}
</span><span style="color: #008080;">20</span> @<span style="color: #008080;">usort</span>(<span style="color: #800080;">$arrayAll</span>, "dimensionsCompare"<span style="color: #000000;">);
</span><span style="color: #008080;">21</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$arrayAll</span>);

 

其他什么的explode还是有点好玩的。

四、字符串操作和正则表达式

字符串整理

<span style="color: #008080;">1</span> <span style="color: #800080;">$name</span>=<span style="color: #008080;">trim</span>(<span style="color: #800080;">$_POST</span>['name'<span style="color: #000000;">]);
</span><span style="color: #008080;">2</span> <span style="color: #800080;">$email</span>=<span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$_POST</span>['email'<span style="color: #000000;">]);
</span><span style="color: #008080;">3</span> <span style="color: #800080;">$feedback</span>=<span style="color: #008080;">chop</span>(<span style="color: #800080;">$_POST</span>['feedback']);

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn