A few highlights of PHP

Source: Internet
Author: User
Tags foreach ereg expression php and php regular expression php template regular expression

I found that 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, and C has all the advantages. Fast, concise, clear, especially the C programmer, PHP is to love, I just love the "php" (Forgot 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:

<?php
$data = array('a' => 'data1', 'b' => 'data2', 'c' => 'data3'); 
while(list($subscript, $value) = each($data)) 

  echo "$subscript => $value :: "; 
  echo "$subscript => $value\n<br>"; 
} 
reset($data); 
foreach($data as $subscript => $value) 

  echo "$subscript => $value :: "; 
  echo "$subscript => $value\n<br>"; 

(2) The variable of the function, the variable of the variable, the "pointer" of the variable: look at the following example:

<?php
//变量的变量
$var = "this is a var";
$varname = "var";
echo $$varname;
//函数的变量
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:

<?php
function($a) {
$a ++;
}
$c = 0;
function($c);
echo $c; //$c仍为0
function(&$a) {
$a ++;
}
$c = 0;
echo $c; //$c为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:

<?php
$pattern = "^010$";
$phone = "01080718828";
if(ereg($pattern, $phone))
echo "打往北京的号";
else
echo "不是打往北京的号";
?> 

This is the regular expression. Beijing's telephone is 8 digits, then I want to know this number is correct? What if he presses the 9-digit number? What if the judgment is correct? This will use the character clusters of the regular expression. 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.

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.