PHP Essentials and tricks that PHP programmers easily ignore

Source: Internet
Author: User
Tags foreach array ereg expression variables php and php regular expression variable

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 language developed from under UNIX, and of course it inherits many of the features of Perl, while C has all the advantages. Fast, concise, clear, especially the C programmer, PHP is to love, I just love the "php" (have forgotten the girlfriend). Here, I want to write a PHP variables, array application techniques and PHP regular expression, PHP template application, and later have time to write PHP and COM, PHP and XML full integration.

1, variable, array of application techniques

(1) Many people use not much of the array function. foreach, list, each. Give a few examples, should be able to know. Cases:

$data = Array (' A ' => ' data1 ', ' B ' => ' data2 ',

' C ' => ' data3 ');

while (the 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) The variable of the function, the variable of the variable, the "pointer" of the variable: look at the following example:

Variable for variables

$var = "This is a Var";

$varname = "Var";

echo $ $varname;

Variables for functions

function Fun1 ($STR) {

Echo $str;

}

$funname = "FUN1";

$funname ("This is a function!");

?>

The "pointer" of the variable. This pointer adds double quotes to indicate that he is not a real pointer. 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 really a pointer, can only be this to understand.

2, Regular expression

Regular expressions are a very big topic, and Perl's regular expressions are so powerful that they are famous. and PHP is not weak, he inherits Perl's regular expression rules, and his own set of rules. Here's just PHP's own regular expression.

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

The start of the regular expression: ^, end of $, between the two symbols is a matching element. If you check a phone number is not a number to Beijing, the regular expression is "^010$". As long as the first 3 area code is 010, is Beijing's number, the back of the phone number will not be the tube. Then, the regular expression matching function ereg is used to judge the example:

$pattern = "^010$";

$phone = "01080718828";

if (Ereg ($pattern, $phone))

echo "hit the number in Beijing";

Else

echo "is not the number of the hit Beijing";

?>

This is the regular expression. The telephone in Beijing is 8 digits, then I want to know that this number is correct? What if he presses the 9-digit number? This will use the character clusters of regular expressions. So the regular expression of the example above is written as follows: ^010[0-9]{8}$, you can also determine whether the number is in accordance with the rules. Regular expressions have many applications, such as LBB, VBB forum in the post of the so-called VBB Code LBB code parsing, are done with regular expressions.

3, Template

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

And with the template, you can achieve the most rationalization of the division of labor. Art only do the page, the program only write backstage, and then together. Excellent JSP provides a custom label feature that completes the template function well. And the mainstream of PHP how to do it? is to use the regular expression to do. can go online to download a phplib, inside the PHP directory has a template.inc source code file, that is the use of PHP to implement the template application class.



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.