This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
This tutorial is just a down and dirty explanation on how to use the include() function in an attempt to make your PHP code-writing life a little easier. Basically the include() function includes the file placed inside the parenthesis as the argument inside of your main file. I use this a lot when I am connecting to a database over multiple files. I keep all of my connection variables in my include file and call them throughout my main scripts. Kind of like this:
Include file: include.php <? // This is my include file $dbHost = "localhost"; $dbUser = "root"; $dbPass = ""; $dbName = "MyDataBase"; ?>
This is the file that I name include.php Its the one that I will be placing inside of my main scripting files. Then all you need to do to place your information inside of your file is one small line of code:
Main file: <? include("include.inc"); // The rest of my code would go here... // and here... // and here... // and here... // and here... ?>
Now there is another function similar to this, require() the only difference between them is that require() returns a fatal error and stops the script from executing. Include() doesn't do that; the script continues to run even if the file that you are including contains some type of an error. I know that this tutorial didn't cover 100% of the bases. It was just a simple explanation. If you would like more info you can check out PHP.net or feel free to ask on the forums in the Server-side Scripting Forum.
|
|
Jubba |
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
:: Copyright KIRUPA 2026 //--