


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
