PHP/MySQL 3-3 (2) _ PHP Tutorial

Source: Internet
Author: User
Tags ereg
PHPMySQL 3-3 (2 ). 3. process regular expressions. let's talk a little bit about using ereg () and eregi () functions to process regular expressions. As I have mentioned earlier, some of these functions are very simple and some are very complicated. For more information, see section 3. process regular expressions.

Let's talk about using the ereg () and eregi () functions to process regular expressions. As I mentioned earlier, some of these functions are simple and complex, depending on your actual needs.

With regular expressions, you can check a string, search for some schema patterns, and determine whether these patterns meet your requirements. The most common usage includes checking whether the email address is valid (of course, even if this method is determined to be valid, there is no guarantee that the email address actually exists ).

We will not detail the complex details of regular expressions here, but just give several examples. You can use the table used in the previous page to copy the corresponding program code and add it to the following code segment to see how it works.

First, make sure that only letters are allowed in each column of the table. The following regular expression is true when the user inputs one or more lower-case letters, and the input number is not allowed:

If (! Ereg ("[a-Z]", $ first) |! Ereg ("[a-Z]", $ last )){

Now let's go further and check whether the string length is four to six characters long. [[: Alpha:] is an easy way to check whether a character is a letter. Check the number of characters in braces. Note that ^ and $ represent the start and end of the string respectively.

If (! Ereg ("^ [: alpha:] {4, 6} $", $ first) |! Ereg ("^ [[: alpha:] {4} $", $ last )){

Finally, we construct a regular expression to verify the validity of the email address. The results of this test method have already been discussed. Nothing is perfect, but the program I provided below is still very effective.

  If (! Ereg (^ [-! # $ % & * + \./0-9 =? A-Z ^ _ 'a-z {|} ~] +.

@.

[-! # $ % & * + \/0-9 =? A-Z ^ _ 'a-z {|} ~] + ..

[-! # $ % & * + \./0-9 =? A-Z ^ _ 'a-z {|} ~] + $, $ Last )){

Don't spend too much time investigating this code. let's go to the next page.

IV. simple method

What about the regular expression above? Interesting, right? It would be interesting to write such a program in every program that needs to check the email address ?! Think about it. you have to write a program that is so messy. you have to write it so many times !... However, of course, there are simpler methods.

Remember before? Have you learned the header file? It allows us to write a program, such as the email address check program, and then include this program into multiple programs. In this way, when we want to rewrite this program, we only need to change one place, and there is no need to modify multiple files.

However, to do this, we must use functions.

We have used functions many times. Every time we query the database or check the string length, we use functions. These functions are provided by PHP. If you are an enthusiastic programmer, you can use your own functions to expand the functions of PHP. However, this part of content is too advanced for this tutorial. The functions we want to create are not those, but the functions written in the PHP script program.

A function is a piece of program code. we can pass one or more values to this code. then this code will process the data we pass to it and return a value. Based on actual needs, functions can be very simple or complex. But as long as we input a number and then we can get a number, you can check whether it is complicated or simple! This is the cute part of the function.

Functions in PHP are similar to those in C. When defining a function, you must specify the data that the function needs to receive. At first, it seems that I don't quite understand why it needs to receive data, but this can prevent some weird problems. The function can do this because all the variables in the function are private variables, that is, they only exist within the function. For example, you have a variable named $ myname in the program. if you create a function and want this function to use the $ myname variable (with the same value), that is not acceptable. You can create a variable named $ myname in the function. These two variables can get along with each other and get different values. But I do not recommend that you do this! If you do, you may be confused when you modify the program six months later.

Now let's create a function. let's make it simple first. We need to give it a name to specify the variable it will receive. Before calling this function, we have to define this function.

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

Function addnum ($ first, $ second ){

$ Newnum = $ first + $ second;

Return $ newnum;

}

Echo addnum (4, 5 );

? $ #@ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

That's all! First, we created the first function of our own. We have defined two new variables, $ first and $ second. pay attention to how they are defined. When calling this function, you need to assign the values-4 to $ first and 5 to $ second in the order they appear. Then we simply add the two numbers together and return the results. "Return" indicates sending the result back. In the last part of the program, we will display the number 9.

Let's create a function to help our database applications. How about a function that can properly handle errors? Try the following program:

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

Function do_error ($ error ){


Echo "Oh, it seems a problem... $ # @ 60; br $ # @ 62 ;";


Echo "system reported errors: $ error. $ # @ 60; br $ # @ 62 ;";


Echo "it is best to temporarily close the website and notify the system administrator. ";

Die;

}


If (! $ Db = @ mysql_connect ("localhost", "user", "password ")){


$ Db_error = "unable to connect to MySQL database ";

Do_error ($ db_error );
}

? $ #@ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

There are two pages in this news. Currently, there are two pages in page 1st.


Before running the program, close the MySQL database or use the wrong user name or password. You will see friendly and useful error messages. Careful friends will notice the @ symbol before the mysql_connect () function. It will suppress system error information, so that the program can only get the relevant error information from the do_error () function. You will also notice that we can pass a variable defined elsewhere as a parameter to the function, instead of assigning a value directly during the call.

Remember that I used private variables in functions? This is not entirely true. In fact, you can allow the function to access variables outside the function. You may need to write a function to query the database and display the result on multiple webpages. You do not want to pass the database connection id to the function every time. In this case, you can define the connection ID as a global variable. For example:

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php

Function db_query ($ SQL ){

Global $ db;

$ Result = mysql_query ($ SQL, $ db );

Return $ result;

}

$ SQL = "SELECT * FROM mytable ";

$ Result = db_query ($ SQL );

? $ #@ 62;

$ # @ 60;/body $ # @ 62;

$ # @ 60;/html $ # @ 62;

This is a simple function, but it is important that you do not have to pass the $ db variable when calling this function-you can use the word global to allow the function to access this variable. You can define multiple global variables in this statement. each global variable is separated by a comma.

Finally, you can use optional parameters, so that you are already a real expert. The key point here is to specify a default value when defining a parameter in a function. When you call this function, if other values are not specified for this parameter variable, the function automatically assigns the default value to this variable. If you specify other values, the default value does not work.

Not quite clear? For example, when you connect to a database, you almost always connect to the same server and use the same user name and password. However, you also need to connect to other servers. Take a look at the following program:

  $ # @ 60; html $ # @ 62;

$ # @ 60; body $ # @ 62;

$ #@ 60 ;? Php



Function db_connect ($ host = "localhost", $ user = "username", $ pass = "graeme "){

$ Db = mysql_connect ($ host, $ username, $ password );

Return $ db;

}


$ Old_db = db_connect ();



$ New_host = "site.com ";

$ New_db = db_connect ($ ne

Handler processes regular expressions. let's talk a little bit about using ereg () and eregi () functions to process regular expressions. As I mentioned earlier, some of these functions are simple and complicated...

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.