


PHP_MySQL Tutorial-Day 3 Basic Functions Page 1/2_PHP Tutorial
Page 1 Basic Functions
Welcome to the third and final lesson of this tutorial. If you have studied the first and second lessons, then you already have the basic knowledge of MySQL and PHP installation and programming. Below we are going to introduce some other functions of PHP that may be useful to you and make your development process easier. First let's take a look at the header file.
Everyone should know some basic concepts of header files, right? A header file is an external file whose contents are included into the main program. The method is also very simple: quote the header file name in the program file, and the header file will be included. Using header files in PHP involves two functions: include() and require(). The difference between these two functions is small, but important, so we need to study it carefully. The require() function works similarly to XSSI; no matter where in the program it is used, the contents of the header file are processed as part of the program itself as soon as the program starts running. Therefore, if you use the require() function in a conditional statement, the header file will be included even if the condition is not true.
The include() function only includes the contents of the header file when this statement is executed. If the program does not run here, PHP will not care about it. This means that when you use include in a conditional part, it will work exactly as you want it to.
Also, if you use the require() function and the header file you specify does not exist, the program will stop running and generate an error. If you use include(), the program will generate a warning message but will continue to run. You can try it yourself, run the following program, then replace include() with require(), and then compare the results of the two programs.
include("emptyfile.inc");
echo "Hello World";
?>
Web page production | website construction | data collection.
I like to name the suffix of the header file as .inc, so that the header file can be distinguished from ordinary programs. If you do the same, please modify the configuration file of the web server software so that it can process the .inc file as a PHP file. Otherwise, hackers may guess the name of your header file and then use the browser to display the contents of the header file in plain text format. At this time, it would be bad if there is some confidential information in your header file (such as database password, etc.).
So, what do you use header files for? Very simple! Put those things that are common to all programs into header files. Like HTML file headers, footnotes, database connection codes, and some functions you define yourself. Copy the following text to a file and save it as header.inc.
$db = mysql_connect("localhost", "root ");
mysql_select_db("mydb",$db);
?>
Then create another file named footer.txt. The file can contain some text and markup that are used at the end of the program.
Now, let’s create another file. This file contains the real PHP program code. Try the following code, of course, you need to confirm that the MySQL database server is running.
$title = "Hello World";
include("header.inc");
$result = mysql_query("SELECT * FROM employees",$db );
echo "
Name | Position |
%s %s | %s |
include("footer.inc");
?>
Did you see what happened? The contents of the header file are merged into the program, and PHP executes all the code. Notice how $title is defined before including the header.inc header file. Its value can be accessed by code in header.inc. In this way, the title of the web page is changed. Now you can use the header.inc header file in any program. All you have to do is give the $title variable an appropriate value in each main program.
With the addition of header files, HTML, conditional statements, and loop statements, you can use the most concise code to write various complex programs with different functions. Header files are more effective when used together with functions, as we will see later.
Next, we will introduce the exciting part: data verification. >>
Second page Data verification
Imagine this situation: We have designed the database properly, and now we ask the user to enter information to write to the database. Suppose you have a field that requires numeric information, such as price; and a lovely user enters text information in this column, causing a failure in the execution of your application. MySQL database refused to accept the text type data you provided in the SQL statement and made a "stern protest" to you.
What to do? You need to use data validation to prevent the above situation from happening.
To put it simply, data verification means that we check the data (usually passed by the user through an HTML form) to see if it follows certain rules. The rules can be diverse, for example, a certain data element cannot be empty, or the content of a certain data item must meet certain requirements (for example, in the previous example, the requirement must be numbers instead of text, or the requirement in the email address Be sure to include an "@" character, etc.).
Data verification can be done on the server side or on the client side. PHP is used for data verification on the server side, while JavaScript or other client-side scripting languages can provide data verification functions on the client side. This article is about PHP, so we focus on server-side verification here. If you want to find some ready-made data verification programs that run on the client, you can check out the NetMonkey library.
Putting the database aside for now, let’s first talk about PHP’s data verification method. If you are willing (or you want to record the data we want to verify), you can add other fields to the employee database created earlier. It is very simple, just use the MySQL ALTER statement.
There are several PHP functions that can be used for data verification, some are very simple, and some are more complex. Among them, strlen() is a relatively simple function, which can tell us the length of a variable.
A bit more complicated is ereg(), this function can process complete regular expressions to perform complex verification. I don't want to go too deep into regular expressions, as many books are devoted to this subject. But I'll give some simple examples on the next page.
Let’s start with a simple example. The following program checks whether a variable exists.
if ($submit) {
if (!$first || !$last) {
$error = "Sorry, you must fill in all fields!"; >if (!$submit || $error) {
echo $error;
?>
} // if end
?>
html>
The key part of this program is the nested conditional statement. The first layer checks if the user pressed the button that sent the data. If so, the program then checks whether both $first and $last variables exist. The || symbol means "or" and the ! symbol means "not". That program description in general language is "If $first does not exist or $last does not exist, then set the $error variable to the following value."
Next, we go one step further and check the length of a piece of text. This is necessary to check user passwords, because you don't want some lazy user to enter a password of only one or two characters, and may be asked to enter a six-digit password.
We have already talked about the strlen() function. It simply returns a number equal to the number of characters contained in the variable being measured. Here, I modify the above program and check the lengths of $first and $last.
The code is as follows:
} else {
// Process form input content
echo "Thank you!";
}
}
if (!$submit || $error) {
echo $error;
?>
} // end of if
?>
You can execute this program and enter six words or less. This check is simple but effective. >>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
