Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1

Figure 15.1 Exiting a loop using break and continue.


Control Structures: include() and require()


PHP gives you the include() and require() statements, allows you to include a second file into the first.
For example, in your first PHP, you may have


include ("file2.php3");

or
require ("file2.php3");
The two commands do the same essential thing but work a little differently. require causes the
contents of the required file to completely replace the require statement. However, require cannot
be used conditionally. In other words, if you want to run the statement more than once and load in a
different file each time, it won't. This is where you need the include statement. include is more
flexible, and you need it if you want to get more clever with specifying what you are including.
include and require can be immensely helpful when building a Web site, even if you use no other
PHP. You might want to put a common header, navigation bar, and footer on every page of your site.
Each Web page would include something like the following:
<HTML>
<HEAD>
<TITLE>My web site, page one</TITLE>
<?php
include ("some_javascript.html");
include ("my_bodytag.html");
include ("my_header.html");
include ("standard_navbar.php3");
?>
<P>Some HTML content here</P>
<?php
include ("standard_footer.html");
?>
That's your page! Each such page would just have the "central" bits customized. You only need to
create a single file for each of the included bits—more importantly, these components are reusable
across your entire Web site.

Functions


PHP gives you the ability to create user-defined functions. These are blocks of code that can be defined
once and invoked from many different places in your code—even from another PHP file.


Apart from user-defined functions, PHP has a vast library of built-in functions. These cover a wide range
of applications, and the limited set covered later in this chapter will give an explanation of those that
relate to the MySQL API.
Free download pdf