Home>

Recently I found a lot of PHP programmers, especially if they haven't studied for a long time.Do not know the essence of PHP.How did Perl become famous in the business world?Its powerful regular expressions.And what about php?He is a language developed from Unix,Of course, it inherits many characteristics of perl,At the same time c has the advantages.Fast, concise and clear, especially for C programmers, PHP is the favorite. Here, I want to write a PHP variable, array application skills and PHP regular expressions, PHP template application.

1.Variable, array application skills

(1) Many people don't use much array functions.foreach, list, each. To give a few examples,You should know.example:

$data=array ("a" =>"data1", "b" =>"data2", "c" =>"data3");
while (list ($subscript, $value)=each ($data))
{
echo "$subscript =>$value ::";
echo "$subscript =>$valuen
";
}
reset ($data);
foreach ($data as $subscript =>$value)
{
echo "$subscript =>$value ::";
echo "$subscript =>$valuen
";
}

(2) Variable of function, variable of variable, "pointer" of variable:See the following example:

//variable variable
$var="this is a var";
$varname="var";
echo $$varname;
//variable of function
function fun1 ($str) {
echo $str;
}
$funname="fun1";
$funname ("this is a function!");
?>

The "pointer" to the variable. This pointer is enclosed in double quotes,Show that he is not a real pointer.Take a look at the following example:

function ($a) {
$a ++;
}
$c=0;
function ($c);
echo $c;//$c is still 0
function (&$a) {
$a ++;
}
$c=0;
echo $c;//$c is 1
?>

The reason why it is called a "pointer" is because it has the same function as a pointer in C.But this is not a real pointer,It can only be understood like this.

Regular expressions

Regular expressions are a very big topic,The power of perl's regular expressions is well-known.And PHP is not weak, he inherited Perl's regular expression rules,Also has its own set of rules.Here only say php's own regular expressions.

Regular expressions are the most basic element.It is simply a set of rules,Used to determine whether other elements conform to their own rules,Or does it have the same feature description.

The beginning of the regular expression:^, the end of $, the two symbols are the matching elements.If you check if a phone number is for Beijing,Using regular expressions means "^ 010 $". As long as the first 3 digits of the area code are 010, it is the number of Beijing,Don't worry about the phone number at the back.Then, use the regular expression matching function ereg to determine, for example:

$pattern="^ 010 $";
$phone="01080718828";
if (ereg ($pattern, $phone))
echo "Number to Beijing";
else
echo "Not a call to Beijing";
?>

This is the regular expression.Phones in Beijing are 8-digit numbers.Then I need to know if this number is correct?What if he pressed the 9-digit number?If the judgment is correct?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} $, you can determine whether the number meets the rules at the same time.There are many applications for regular expressions,Like the analysis of the so-called vbb code and bbb code when posting on the LBB and VBB forums,This is done using regular expressions.

3.Template

Knowing the function of regular expressions,Then you can know the template.What is a template?For example?When writing background pages using background programs,Program code is inserted in the web page.Such as php. This is a mix of html and php. This has the advantage of fast reading speed,The disadvantage is that if everyone cooperates to make a website,Then non-programmers will not change the net.

And using templates,The most rationalization of division of labor can be achieved.The artist only makes pages,The program only writes in the background,Then close them together.The excellent jsp provides the function of custom tags to complete the template function well.And how does mainstream php do it?This is done using regular expressions.You can go online to download a phplib, there is a template.inc source code file in the php directory,That's the class that PHP uses to implement the template.

The above is some experience.Very practical, the purpose is to let everyone better understand the language of php programming,I hope you can gain something.

php
  • Previous C # method for controlling system volume and mute based on plus and minus button form
  • Next Summary of ways to delete and empty sessions in PHP