Page 3: Processing regular expressions
Let's briefly talk about using two functions ereg() and ereg() to process regular expressions. As I mentioned earlier, some of these functions are very simple, some are very complicated, depending on your actual needs.
Using regular expressions, you can check a string, search for some of the structural patterns, and determine whether these patterns meet your regulations. The most common usage includes checking whether the email address is valid (of course, even if this method determines that it is valid, it cannot guarantee that the email address really exists).
We will not look at the complex details of conventional expressions here, and only give a few examples. You can use the table used in the previous page - Copy the corresponding program code and add it to the code snippet below to see how it works.
First, we need to make sure that only letters can be entered in each column in the table. The following regular expression is determined to be true when the user enters one or more lowercase letters, and the input of numbers is not allowed:
if (!ereg("[a-Z]", $first) || !ereg("[a-Z]", $last)) {
Now let's go a step further and check if the length of the string is four to six characters long. Using [[:alpha:]] is an easy way to check whether a character is a letter or not. The number in braces checks the number of characters. It should also be noted 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 check the validity of the email address. The effect of this test method has sparked considerable discussion. Nothing is perfect, but the program I gave below works very well.
I got this baby program from the PHP email discussion group. That's a good place - go and check it out often. OK, this program looks a bit messy.
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 exploring this code, but go to the next page first. >>
Page 4 Easy Method
How about the previous regular expression? Very interesting, right? What would be interesting if I wrote such a program in every program that needs to check the email address? ! Think about it, you have to write such a messy program, and you have to write it so many times! ...But, of course, there are easier ways.
Do you still remember the header files we learned before? It allows us to write a program, like a checking program for this email address, 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 without modifying multiple files.
However, to do this, we must use functions.
We've used functions many times. Every time we query the database or check the length of a string, we use functions to do it. These functions are provided by PHP. If you are an enthusiastic programmer, you can use functions you write yourself to expand the functionality of PHP itself. But for this tutorial, this part of the content is a bit too profound. The function we want to create is not that type of function, but a function written inside the PHP script program.
A function is a piece of program code. We can pass one or more values to this code, and the code will process the data we pass it to and return a value. Depending on actual needs, functions can be simple or very complex. But as long as we pass in a number and then we can get a number, please do you care whether it is complicated or simple! This is the cuteness of the function.
Functions in PHP perform similarly to those in C language. When we define a function, we must indicate what kind of data the function needs to receive. At first it seems that it is not easy to understand why it needs to receive data, but this can prevent some weird problems. The reason why a function can do this is because the variables in the function are all private variables, that is, they only exist inside the function. For example, you have a variable in your program called $myname. If you create a function and want this function to use that $myname variable (the value is also the same), that is not possible. You can create a variable inside the function, also called $myname. These two variables can be separated by each other and take different values. But I don't recommend you do this! If you really do this, you may be confused when you come to modify such a program in half a year.
Then let’s create a function now and start with a simple one. We want to give it a name and specify what variable it wants to receive. Before calling this function, we have to define this function.
Copy the codeThe code is as follows:
<html>
<body>
<?php
function addnum($first, $second) {
$newnum = $first + $second;
return $newnum;
}
echo addnum(4,5);
?>
</body>
</html>
That's all! First, we created our first function. We define two new variables, $first and $second, and note how they are defined. When calling this function, assign the values to these two variables in the order in which they appear. - 4 to $first and 5 to $second. Then we simply add these two numbers together and return the result. "Return" here means sending the result back. In the last part of the program, we display the number 9.
Let's create another function to make it a little helpful for our database application. How about a function that can handle errors properly? Try the following program:
Copy the codeThe code is as follows:
<html>
<body>
<?php
function do_error($error) {
echo "Oh, it seems to be a little problem...<br>";
echo “The system reported error is: $<br>”;
echo “It is better to temporarily close the website and notify the system administrator.”;
die;
}
if (!$db = @mysql_connect("localhost","user", "password")) {
$db_error = "Cannot connect to MySQL database";
do_error($db_error);
}
?>
</body>
</html>
Before running the program, try closing the MySQL database, or using the wrong username or password. You will see friendly, useful error messages. Careful friends will notice the @ symbol before the mysql_connect() function. It suppresses system error information, so that the program can only get relevant error information from the do_error() function. You will also notice that we can pass a variable defined elsewhere as an argument to the function instead of assigning a value directly when called.
Remember when I used private variables, right? This is not completely true. In fact, you can let the function access variables outside the function. You might want to write a function, use it to query the database, and then display the results on multiple web pages. You don't want to pass the database connection identifier to the function every time. In this case, you can define the connection identity as a global variable. For example:
Copy the codeThe code is as follows:
<html>
<body>
<?php
function db_query($sql) {
global $db;
$result = mysql_query($sql,$db);
return $result;
}
$sql = "SELECT * FROM mytable";
$result = db_query($sql);
?>
</body>
</html>
This is a very simple function, but the important thing is that when you call this function, you don't have to pass the $db variable - you can use the word global to make the function access to the variable. In this statement you can define multiple global variables, separated by commas.
Finally, you can use optional parameters so that you look like you are already a real expert. The key point here is that when defining parameters in a function, you must specify a default value to them. Then when you call this function, if you do not specify other values for the parameter variable, the function will automatically assign the default value to the variable. If you specify another value, the default value does not work.
Don't quite understand? For example, when you connect to the database, you almost always connect to the same server and use the same username and password. Sometimes, though, you also need to connect to another server. Check out the following program:
Copy the codeThe code is as follows:
<html>
<body>
<?php
function db_connect($host = "localhost", $user="username", $pass="graeme") {
$db = mysql_connect($host, $username, $password);
return $db;
}
$old_db = db_connect();
$new_host = "";
$new_db = db_connect($new_host);
?>
</body>
</html>
Think about where you can use functions. You can use functions to verify data, complete commonly used functions, and so on. I used a lot of functions when processing text displayed on web pages. I can check, parse and modify the text at once to add newlines and HTML tags, etc.
Now, what I'm leaving behind is some advice I want to give you. >>
Page 5 Advanced skills
When it comes to database development, we have a lot to learn. If you have not learned how to design a database and how to reliably run a database on different platforms, please find a good book in this area to read it. This ability will bring you immeasurable benefits, and from a long-term perspective, it will save you a lot of time and energy. Also, study MySQL seriously. This is a complex and interesting database with lots of good documentation. Learn about the table structure, data type, and SQL of the database. If you really have SQL in hand, you can do quite a bit of practical work.
Finally, there is PHP. Almost everything you want can be found on PHP's website, including comprehensive documentation, email discussion content, program code base, and more. A great way to learn PHP is to study the examples given in the user manual and consult the code online. The code posted by netizens includes many functions and classes that you can use directly in your own program without having to start over. Also, if you have problems, the email discussion group is a very worthwhile resource. PHP developers themselves will also participate in the email discussion group, and there are many experienced experts who can help you solve your problems.
I wish you a smooth programming and all the best!
Previous page12Read the full text