PHP
By Web Monkey
Page 1 A Place for Everything
--------------------------------------------------------------------------------
Welcome to the third and final lesson for this tutorial. If
you've gone through Lesson 1 and Lesson 2, you already know
the essentials for installing and writing useful scripts with
MySQL and PHP. We're going to look at some useful PHP functions
that should make your life a lot easier. First, let's look
at include files.
We all know the basics of includes, right? Contents of an
external file are referenced and imported into the main file.
It's pretty easy: You call a file and it's included. When
we do this in PHP there are two functions we need to talk
about: include() and require(). The difference between these
two functions is subtle but important, so let's take a closer
look. The require() function works in a XSSI-like way; files
are included as part of the original document as soon as that
file is parsed, regardless of its location in the script.
So if you decide to place a require() function inside a conditional
loop, the external file will be included even if that part
of the conditional loop is false.
The include() function imports the referenced file each time
it is encountered. If it's not encountered, PHP won't bother
with it. This means that you can use include in loops and
conditional statements, and they'll work exactly as planned.
Finally, if you use require() and the file you're including
does not exist, your script will halt and produce an error.
If you use include(), your script will generate a warning,
but carry on. You can test this yourself by trying the following
script. Run the script, then replace include() with require()
and compare the results.
<html>
<body>
<?php
include("emptyfile.inc");
echo
"Hello World";
?>
</body>
</html>
I like to use the suffix .inc with my include files so I can
separate them from normal PHP scripts. If you do this, make
sure that you set your Web server configuration file to parse
.inc files as PHP files. Otherwise, hackers might be able
to guess the name of your include files and display them through
the browser as text files. This could be bad if you've got
sensitive information - such as database passwords - contained
in the includes.
So what are you going to do with include files? Simple! Place
information common to all pages inside them. Things like HTML
headers, footers, database connection code, and user-defined
functions are all good candidates. Paste this text into a
file called header.inc.
<?php
$db
= mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
?>
<html>
<head>
<title>
<?php
echo $title ?>
</title>
</head>
<body>
<center><h2><?php
echo $title ?></h2></center>
Then create another file called footer.txt that contains some
appropriate closing text and tags.
Now let's create a third file containing the actual PHP script.
Try the following code, making sure that your MySQL 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>Name</td><td>Position</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");
?>
See what happens? The include files are tossed into the main
file and then the whole thing is executed by PHP. Notice how
the variable $title was defined before header.inc is referenced.
Its value is made available to the code in header.inc; hence,
the title of the page is changed. You can now use header.inc
across all your PHP pages, and all you'll have to do is change
the value of $title from page to page.
Using a combination of includes, HTML, conditional statements,
and loops, you can create complex variations from page to
page with an absolute minimum of code. Includes become especially
useful when used with functions, as we'll see down the road.
On to the exciting world of data validation.
next page»
|