PROGRAMMING COURSES ADMIN VIDEO TUTORIALS COMMUNITY RESOURCES |
When you use the include(), include_once(),
require(), or require_once()
statements in PHP you are instructing a certain file and all of its
contents to be "included" into your script that is calling it. This is
very handy for making modular reusable code structures, even in simple
applications.
The include() statement will include the desired file if the file can be found through the path specified in the statement. If the desired file cannot be found, no errors will occur, but the file and its contents will not be included. The same file can be included multiple times into the calling script. Using include_once() you can command the calling script to only include a certain file one time and no more. The require() statement works a lot like the include() statement in the respect that will bring another file and its contents into the calling script. The big difference is that using require() will "require" the file to be at the specified location on server. If the desired file to require cannot be found, an error will occur in your script, and the script will terminate. Using require_once() you can command the calling script to only require a certain file one time and no more. For the following code examples we first need a file to include, let's create that now: Name this file "my_file.php" and make sure it is living in the same directory as the calling file: ![]() <?php
Now let's include that file into our main calling file by doing the following: ![]() <?php
Hello World!
Here is an example of using each type: ![]() <?php
Here are alternate ways of writing your include and require statement values that all work the same: ![]() <?php
|
|||||||||||
|
|
DevelopPHP Requires Flash Player
___________________________________________
|
||||||||