Including Files
Chris on 2001 May 21
Chris on 2001 May 21
Another thing we can do is "include" files. This comes in handy when you have the same information on all your pages, over and over again. For example, connecting to the same database, printing the same HTML at the top or bottom of a page. It can be used for headers or footers, or for simplifying things. There are endless possibilities for this. How do we use it?
<html>
<body>
<?php
include ("example.inc");
?>
</body>
</html>
Now, in your "example.inc" file, if something like this was put in -
<?php
$hello = "Hello World";
echo $hello;
?>
It would be the same as putting it all in the one file. The only problem is that you have to put the PHP delimiters in the included file, otherwise it will just display everything that is in there. You can actually name the file to include anything you want, using .inc just separates them from the other PHP scripts.
Another thing we can do is -
<?php
require ("example.inc");
?>
What's the difference? If the required file doesn't exist, then when you run the script it will stop, and give an error. If its included instead, it will display a warning and then continue with the script. In our example, the following errors give an example of what could happen.
This is for the included file -
Warning: Failed opening 'hello.inc' for inclusion (include_path='') in /tutorial/9.php on line 6
This is for the required file -
Fatal error: Failed opening required 'hello.inc' (include_path='') in /tutorial/9.php on line 6
See the slight difference? For the required file it gives a "Fatal Error", for the included file it gives a "Warning".
The only problem with using an "included" file is if we put passwords in there. Since PostgreSQL doesn't use passwords when connecting to a database (at least in our example), it's not so important. In our later examples, we'll just make one file to include with all of our information in it.
There are lots more things that we can do with PHP and PostgreSQL, or just PHP by itself.
That's it for now, until I can think of some ideas to put in here. E-Mail Me for suggestions for the tutorial, or if there are any problems with it.
