Home >Backend Development >PHP Tutorial >Summary of PHP template application skills_PHP tutorial
After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss it with everyone. I found that many PHP programmers, especially those who have not studied for a long time, do not know the essence of PHP. How did Perl become famous in the business world back then? Its powerful regular expressions. And what about PHP? It is a language developed under Unix. Of course, it inherits many features of Perl and has the advantages of C.
Here, I want to write an article about PHP variables, array application skills, PHP regular expressions, and PHP template applications. I will write about the complete combination of PHP and COM, PHP and XML when I have time in the future.
1. PHP template
2. Application skills of variables and arrays
(1) Array functions that are rarely used by many people. foreach, list, each. Just give a few examples and you should be able to figure it out. Example:
<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 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>></span></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><SPAN class=tag-name>br</SPAN><SPAN class=tag>></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><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>"; </span> </li> <li class="alt"><span>} </span></li> </ol>
(2) Variables of functions, variables of variables, "pointers" of variables: See the following example:
<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 class=""><SPAN>//变量的变量 </SPAN><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></SPAN><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></SPAN><LI class=alt><SPAN>echo $$varname; </SPAN><LI class=""><SPAN>//函数的变量 </SPAN><LI class=alt><SPAN>function fun1($str) { </SPAN><LI class=""><SPAN>echo $str; </SPAN><LI class=alt><SPAN>} </SPAN><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></SPAN><LI class=alt><SPAN>$funname("This is a function !"); </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
"Pointers" of variables. This pointer is enclosed in double quotes, indicating that it is not a real pointer. Take a look at the following example:
<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 class=""><SPAN>function($a) { </SPAN><LI class=alt><SPAN>$a ++; </SPAN><LI class=""><SPAN>} </SPAN><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></SPAN><LI class=""><SPAN>function($c); </SPAN><LI class=alt><SPAN>echo $c; //$c仍为0 </SPAN><LI class=""><SPAN>function(&$a) { </SPAN><LI class=alt><SPAN>$a ++; </SPAN><LI class=""><SPAN>} </SPAN><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></SPAN><LI class=""><SPAN>echo $c; //$c为1 </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
The reason why it is called a "pointer" is because it has the same function as a pointer in C language. But this is not a real pointer, it can only be understood in this way.
3. Regular expressions
Regular expressions are a very big topic, and Perl’s regular expressions are well-known for their power. PHP is not weak either. It inherits Perl's regular expression rules and has its own set of rules. Here we only talk about PHP's own regular expressions. Regular expressions are the most basic element. Simply put, it is a set of rules used to determine whether other elements conform to its own rules, or whether they have the same characteristic description. The starting symbol of the regular expression: ^ and the ending symbol $. The elements between these two symbols are matched. For example, if you want to check whether a phone number is for Beijing, the regular expression would be "^010$". As long as the first three digits of the area code are 010, it is Beijing's number, and the following phone numbers are not needed. Then, use the regular expression matching function ereg to judge, for example:
<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 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></SPAN><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></SPAN><LI class=""><SPAN>if(ereg($pattern, $phone)) </SPAN><LI class=alt><SPAN>echo "打往北京的号"; </SPAN><LI class=""><SPAN>else </SPAN><LI class=alt><SPAN>echo "不是打往北京的号"; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
This is the regular expression. Phone numbers in Beijing are all 8 digits, so I want to know if the number is correct? What if he pressed the 9-digit number? If judged right or wrong? This requires the use of regular expression character clusters. Then the regular expression in the above example should be written like this: ^010[0-9]{8}$, and it can be judged at the same time whether the number conforms to the rules. Regular expressions have many applications. For example, the so-called VBB code analysis in LBB and VBB forums when posting is all done using regular expressions.