PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解,和大家讨论讨论。我发现很多的PHP程序员,尤其是学习还不是很久的,都不知道PHP的精华所在。Perl当年如何在商界出名?其强大的正则表达式。而PHP呢?他是一门从Unix下发展起来的语言,当然也就继承了Perl的很多特点,同时C的优点都有。
这里,我想来写一篇PHP的变量、数组应用技巧和PHP的正则表达式、PHP模板应用,以后有时间再写PHP与 COM、PHP与XML的完全结合。
1、PHP模板
2、变量、数组的应用技巧
(1)很多人用得不多的数组函数。foreach、list、each。分别举几个例子,应该就能知道了。例:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('a' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'data1', 'b' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'data2', 'c' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'data3'); </span> </li> <li class="alt"><span>while(list($subscript, $value) = each($data)) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>echo "$</span><span class="attribute"><font color="#ff0000">subscript</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value :: "; </span> </li> <li class=""> <span>echo "$</span><span class="attribute"><font color="#ff0000">subscript</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $valuen</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>reset($data); </span></li> <li class="alt"> <span>foreach($data as $</span><span class="attribute"><font color="#ff0000">subscript</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>echo "$</span><span class="attribute"><font color="#ff0000">subscript</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value :: "; </span> </li> <li class=""> <span>echo "$</span><span class="attribute"><font color="#ff0000">subscript</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $valuen</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class="alt"><span>} </span></li> </ol>
(2)函数的变量、变量的变量、变量的“指针”:看下例:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>//变量的变量 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">var</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"this is a var"</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">varname</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"var"</font></span><span>; </span> </li> <li class="alt"><span>echo $$varname; </span></li> <li class=""><span>//函数的变量 </span></li> <li class="alt"><span>function fun1($str) { </span></li> <li class=""><span>echo $str; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">funname</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"fun1"</font></span><span>; </span> </li> <li class="alt"><span>$funname("This is a function !"); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
变量的“指针”。这个指针加上了双引号,表明他不是真正的指针。看看下例:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>function($a) { </span></li> <li class="alt"><span>$a ++; </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">c</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li class=""><span>function($c); </span></li> <li class="alt"><span>echo $c; //$c仍为0 </span></li> <li class=""><span>function(&$a) { </span></li> <li class="alt"><span>$a ++; </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">c</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li class=""><span>echo $c; //$c为1 </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
之所以称其为“指针”,就是因为他有了和C语言中指针相同的功能。但这又不是真正的指针,只能够是这样的去理解。
3、正则表达式
正则表达式是一个非常大的题目,Perl的正则表达式的强大是闻了名的。而PHP也不弱,他继承了Perl的正则表达式法则,还有自己的一套法则。这里只说PHP自己的正则表达式。正则表达式是最基本的元素。简单地说就是一套规则,用于去判定其它的元素是不是符合自身的规则,或者说是不是有相同的特征描述。正则表达式的开始符:^,结尾符$,这两个符号间的是匹配的元素。如检查一个电话号码是不是打往北京的号,用正则表达式表示就是“^010$”。只要前3位区号是010,就是北京的号,后面的电话号码就不用管了。然后,用正则表达式匹配函数ereg来判断,例:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">pattern</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"^010$"</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">phone</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"01080718828"</font></span><span>; </span> </li> <li class=""><span>if(ereg($pattern, $phone)) </span></li> <li class="alt"><span>echo "打往北京的号"; </span></li> <li class=""><span>else </span></li> <li class="alt"><span>echo "不是打往北京的号"; </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
这就是正则表达式。北京的电话都是8位数字的,那我要知道这个号码是不是正确了?假如他按了9位号呢?如果判断正误?这就要用到正则表达式的字符簇。那么上例的正则表达式就要这样写:^010[0-9]{8}$,就能同时判断号码是不是符合规则。正则表达式有很多的应用,像LBB、VBB论坛在发贴时的所谓VBB代码LBB代码的解析,都是用正则表达式完成的。