First, the basic function
Welcome to the third and final lesson of this tutorial. If you've learned the first and second lessons, you've mastered the basics of installing and programming MySQL and PHP. Here are some of the other functions of PHP that might be useful to you and make your development process simpler. First, let's look at the document.
You should know some basic concepts of the header file, right? The header file is an external file whose contents are included in the main program. The method is also very simple: referencing the header filename in the program file, the header file is included. Using a header file in PHP involves two functions: include () and require (). These two functions are very small, but very important, so we have to study carefully. The Require () function works like a xssi, and regardless of which part of the program 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 decision statement, the header file is included even if the condition is not true.
The Include () function only contains the contents of the head file when executing to this statement. If the program is not running here, PHP will not be able to control it. This means that when you use include in the Conditional decision section, it works exactly the way you want it to.
Also, if you use the Require () function and the header file you specified does not exist, the program will stop running and produce an error. If you use include (), the program will produce a warning message, but it will continue to run. You can try it yourself, run the following program, and then replace the include () with require () and compare the results of the two programs.
<body>
<?php
include("emptyfile.inc");
echo "Hello World";
?>
</body>
I like the suffix name of the head file as. Inc, so that you can distinguish the head file from the general program. If you do the same, please modify the Web server Software configuration file so that it can handle the. inc file as a PHP file. Otherwise, hackers may guess your header file name, and then use the browser to display the contents of the head file in plain text format. At this point, if you have some confidential information (such as a database password, etc.) in your header file, that would be bad.
So, what do you do with a header file? Very simple! Put the content that is common to all programs in the header file. Like HTML file headers, footnotes, database connection codes, and some of the functions you've defined yourself. Copy the following text into a file and save it as Header.inc.
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
?>
<title>
<?php echo $title ?>
</title>
<body>
<center>
Then create another file, the name is Footer.txt, which can contain some of the text and tags used at the end of the program.
Now we're going to create a file with the actual PHP code inside the file. Try the following code, of course, to confirm that the MySQL database server is running.
<?php
$title = "Hello World";
include("header.inc");
$result = mysql_query("SELECT * FROM employees",$db);
echo "<table border=1>\n";
echo "<tr><td>名字</td><td>职位</tr>\n";
while ($myrow = mysql_fetch_row($result)) {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow[1], $myrow[2], $myrow[3]);
}
echo "</table>\n";
include("footer.inc");
?>
Do you see what's going on? The contents of the header file are merged into the program, and PHP executes all the code. Notice how the $title is defined before the Header.inc header file is included. The code in HEADER.INC can access its value. This way, the title of the page is changed. Now that you can use the Header.inc header file in any program, all you have to do is to get the appropriate value for the $title variable in each main program.
header files, HTML, conditional decision statements, and looping statements, which are added to some, you can use the most concise code to write various complex programs of varying functions. When used in conjunction with a function, the header file is more effective, as we'll see later.