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

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.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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

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 remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.