Examples of the essence and techniques of PHP language _php tutorial

Source: Internet
Author: User
Tags ereg perl regular expression php regular expression php template

A lot of PHP programmers, especially learning is not a long time, do not know the essence of PHP. How did Perl become famous in the business world? Its powerful regular expression. and PHP? He is a language developed from the UNIX, of course, has inherited many features of Perl, while the advantages of C have. Fast, concise, clear, especially C programmer, PHP is to love, I just Love "PHP" (have forgotten my girlfriend). Here, I would like to write a PHP variable, array application techniques and PHP regular expression, PHP template application, there will be time to write PHP and COM, PHP and the full integration of XML.

1. Application techniques of variables and arrays

(1) A lot of people do not use many array functions. foreach, list, each. To give a few examples, we should be able to know. Cases:

$data = Array (a = data1, B = data2, c = data3);
while (list ($subscript, $value) = each ($data))
{
echo "$subscript + = $value::";
echo "$subscript and $valuen
";
}
Reset ($data);
foreach ($data as $subscript = $value)
{
echo "$subscript + = $value::";
echo "$subscript and $valuen
";
}

(2) A variable of a function, a variable of a variable, a "pointer" to a variable:

Variables of variable
$var = "This is a Var";
$varname = "Var";
echo $ $varname;
Variables of the function
function Fun1 ($STR) {
Echo $str;
}
$funname = "FUN1";
$funname ("This is a function!");
?>

The "pointer" of the variable. This pointer adds double quotes, indicating 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
?>

It is called "pointer" because he has the same function as the pointer in C language. But this is not the real pointer, only can be this to understand.

2. Regular expressions

Regular expressions are a very large topic, and the power of Perl's regular expressions is famous. and PHP is not weak, he inherited the Perl regular expression law, and his own set of rules. This is just about PHP's own regular expression.

Regular expressions are the most basic element. In short, it is a set of rules for determining whether other elements are in accordance with their own rules, or whether they have the same feature description.

The starting character of the regular expression: ^, end character $, between the two symbols is a matching element. If you check whether a phone number is called to Beijing, the regular expression means "^010$". As long as the first 3 digits of the area code is 010, is the number of Beijing, the phone number on the back of the tube. Then, use the regular expression matching function ereg to judge, for example:

$pattern = "^010$";
$phone = "01080718828";
if (Ereg ($pattern, $phone))
echo "number to Beijing";
Else
echo "is not a number to Beijing";
?>

This is the regular expression. The telephone in Beijing is 8 digits, then I need to know if this number is correct? What if he presses the 9-digit number? This will use the regular expression of the character cluster. So the regular expression in the example above is written like this: ^010[0-9]{8}$, you can also judge whether the number is in accordance with the rules. Regular expressions have a lot of applications, like the LBB, VBB forum in the post of the so-called VBB Code LBB Code of the resolution, are used regular expression to complete.

3. Templates

Knowing the function of the regular expression, you can know the template. What is a template? for example, when a Web page is used in a background program, it is inserted into the program code. such as PHP. This is the mix of HTML and PHP. This is the advantage of fast reading speed, the disadvantage is that if we work together to do the site, then non-programmers will not change the network.

and using the template, you can achieve the most rationalization of division of labor. The art only makes the page, the program only writes the backstage, then then together. The excellent JSP provides a custom label function that completes the template function well. And how does the mainstream PHP do it? is using regular expressions to do it. can go to the Internet to download a phplib, inside the PHP directory has a template.inc source code file, that is, PHP implementation of the template to apply the class.

http://www.bkjia.com/PHPjc/508356.html www.bkjia.com true http://www.bkjia.com/PHPjc/508356.html techarticle a lot of PHP programmers, especially learning is not a long time, do not know the essence of PHP. How did Perl become famous in the business world? Its powerful regular expression. And what about PHP? He is a door from ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.