Examples of the essence and skills in PHP

Source: Internet
Author: User
Tags ereg php template

Many PHP programmers, especially those who have not been learning for a long time, do not know the essence of PHP. How did Perl become famous in the business world? Its powerful regular expression. What about PHP? He is a language developed from Unix. Of course, he inherits many features of Perl and has advantages in C. Fast, concise, and clear. Especially for C programmers, PHP is a favorite. I love PHP very much (I forgot my girlfriend ). Here, I want to write an article about PHP variables, Array application skills, PHP regular expressions, and PHP template applications. I will try again later to write the full combination of PHP, COM, PHP, and XML.

1. Application Techniques of variables and Arrays

(1) A lot of array functions are rarely used. Foreach, list, and each. Here are a few examples. 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) function variables, variable variables, and variable "Pointers ":

// Variable
$ Var = "this is a var ";
$ Varname = "var ";
Echo $ varname;
// Function variable
Function fun1 ($ str ){
Echo $ str;
}
$ Funname = "fun1 ";
$ Funname ("This is a function! ");
?>

The "Pointer" of the variable ". This pointer is enclosed in double quotes, indicating that it is not a real pointer. Take a look at the following example:

Function ($ ){
$ A ++;
}
$ C = 0;
Function ($ c );
Echo $ c; // $ c is still 0
Function (& $ ){
$ A ++;
}
$ C = 0;
Echo $ c; // $ c is 1
?>

It is called "pointer" because it has the same function as the pointer in C language. But this is not a real pointer. It can only be understood in this way.

2. Regular Expression

Regular Expressions are a very big question. Perl's regular expressions are powerful and famous. PHP is not weak. It inherits the regular expression rules of Perl and has its own set of rules. Here we only talk about PHP regular expressions.

Regular Expressions are the most basic elements. Simply put, it is a set of rules used to determine whether other elements comply with their own rules, or whether they have the same feature description.

The starting character of the regular expression: ^, ending character $. The two symbols are matched elements. If you check whether a phone number is sent to Beijing, use a regular expression to indicate "^ 010 $ ". As long as the first three area codes are 010, that is, the Beijing number, the phone number at the end will be no longer needed. Then, use the regular expression to match the ereg function. For example:

$ Pattern = "^ 010 $ ";
$ Phone = "01080718828 ";
If (ereg ($ pattern, $ phone ))
Echo "number to Beijing ";
Else
Echo "not the number for Beijing ";
?>

 

This is a regular expression. The numbers in Beijing are 8 digits. Do I need to know if this number is correct? What if he presses the 9-digit number? If it is true or false? This requires the use of regular expressions in character clusters. In the above example, the regular expression should be written as follows: ^ 010 [0-9] {8} $ to determine whether the number meets the rules at the same time. Regular expressions have many applications, such as the parsing of the so-called VBB code LBB code in the LBB and VBB forums.

3. Template

Now that you know the regular expression function, you can know the template. What is a template? For example? When a webpage is used as a background program, the program code is inserted into the webpage. Such as PHP. This is the hybrid Writing of HTML and PHP. This advantage is that the reading speed is fast, and the disadvantage is that if you work together to build a website, non-programmers will not change the network.

With templates, the most reasonable division of labor can be achieved. The artist only serves as the page, and the program only writes to the background, and then combines them. Excellent Jsp provides the custom tag function to complete the template function. How does the mainstream PHP achieve this? This is done using regular expressions. You can download a PHPLIB file from the Internet. The PHP Directory contains a source code file of template. inc, which is the class used to implement template application using PHP.

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.