Php/mysql 3rd Pass-the third day (ii) _php tutorial

Source: Internet
Author: User
Tags ereg
III. handling of regular expressions

Let's talk a little bit about using ereg () and eregi () two functions to handle regular expressions. As I mentioned earlier, some of these functions are simple, some are complex, and are determined by your actual needs.

With regular expressions, you can examine a string and search for some of its structural patterns to determine whether these patterns meet your requirements. The most common usage includes checking that the e-mail address is valid (and, of course, even if the method is judged to be valid, there is no guarantee that the email address really exists).

We don't scrutiny the complex details of regular expressions here, just a few examples. You can use the table from the previous page-copy the corresponding program code and add it to the code snippet below to see how it works.

First, we want to make sure that the columns in the table can only enter letters. The following general expression is determined to be true when the user enters one or more lowercase letters, and the input number is not allowed:

if (!ereg ("[A-Z]", $first) | |!ereg ("[A-Z]", $last)) {

Now let's go further and check if the length of the string is four to six characters long. using [[: Alpha:]] is a simple way to check if a character is not a letter. The number of characters in the curly braces is checked. Also note that ^ and $ represent the beginning and end of the string, respectively.

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

Finally, let's construct a regular expression to verify the validity of the e-mail address. The effect of this kind of test has aroused quite a lot of discussion. Nothing is perfect, but the procedure I gave 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 to scrutiny this piece of code, or first to the next page of content.

Four, the Simple method

What about the previous regular expression? It's interesting, isn't it? Is it really interesting to write a program in every program that needs to check the email address?! Think about it, you have to write such a messy program, you have to write so many times! ... But, of course, there's a simpler way.

You remember the front? Did you learn the header files? It allows us to write a program, like the check-in for this email address, and then include the program in multiple programs. In this way, when we rewrite this procedure, we just need to change one place, without having to modify multiple files.

But to do this, we have to use the function.

We've already used many functions. Every time we query a database or check the length of a string, we use functions to do it. These functions are self-bringing with PHP. If you are an avid programmer, you can use your own functions to augment the functionality of PHP itself. But for this tutorial, this part is a bit too advanced. The function we are going to create is not that one, but a function written inside the PHP script.

A function is a program code, we can pass one or more values to this code, and then this code will process the data we pass to it and return a value. Functions can be very simple or complex, depending on the actual needs. But as long as we pass in a number, and then can get a number, you Tube it is complex or simple it! This is where the function is lovely.

The function in PHP is similar to the function in C language. When we define a function, we must indicate what data the function needs to receive. At first it didn't seem so good to understand why it was going to receive data, but that would prevent some weird problems. The function can do this because the variables inside the function are private variables, that is, it exists only within the function. For example, you have a variable called $myname in your program, and if you create a function that wants the function to use that $myname variable (the same value), that is not possible. You can create a variable inside the function, named $myname, which can be used to get along with each other and take different values. But I don't suggest you do this! If you do this, you may get confused when you change the program after six months.

So let's create a function now, let's start with a simple one. We're going to give it a name and specify what variables it's going to receive. We have to define this function before calling 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;

It's all right! First, we created our first own function. We define two new variables, $first and $second, and notice how they are defined. When calling this function, give the two variables a good value in the order in which they appear-4 assigns the $first,5 to $second. Then we simply add the two numbers together and return the results. "Back" here means to send the results back. In the last part of the program we show the number 9.

Let's create a function to make it a little bit more useful for our database application. What about a function that can handle mistakes properly? Try the following program:

  $#@60;html$#@62;

$#@60;body$#@62;

$#@60;? Php

function Do_error ($error) {


echo "Oh, it seems a bit of a problem ... $#@60;br$#@62;";


echo "System reported errors are: $error. $#@60;br$#@62;";


echo "It is best to temporarily close the site 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;

This news a total of 2 pages, currently on the 1th page 1 2


Before running the program, try shutting down the MySQL database or using the wrong user name or password. You will see a friendly, useful error message. A careful friend will notice the @ sign before the mysql_connect () function. It suppresses system error messages so that the program can only get information about the error from the Do_error () function. You'll also notice that we can pass a variable defined elsewhere as an argument to a function instead of assigning a value directly to the call.

Remember when I used a private variable for my function? That's not exactly the right words. In fact, you can have the function access variables outside of the function. You might want to write a function that uses it to query the database and then display the results in multiple pages. You do not want to pass the database connection identity to the function every time. In this case, you can define the connection identifier 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 very simple function, but it is important that when you call this function, you do not have to pass the $DB variable-you can use the word global to make the function accessible to that variable. In this statement, you can define multiple global variables, separated by commas from each global variable.

Finally, you can use optional parameters so that you look like you're a real expert. The key point here is to specify a default value when defining a parameter in a function. Then, when you call this function, if you do not specify a different value for the parameter variable, the function automatically assigns the default value to the variable. If you specify a different value, the default value will not work.

Don't understand? For example, when you connect to a database, you almost always connect to the same server and use the same user name and password. Sometimes, however, you need to connect to other servers as well. 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

http://www.bkjia.com/phpjc/ 532609.html www.bkjia.com true http://www.bkjia.com/phpjc/532609.html techarticle Iii. Working with regular expressions Let's talk a little bit about using ereg () and eregi () two functions to handle regular expressions. As I mentioned earlier, some of these functions are very simple, some are very complex, look at your ...

  • 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.