Fork me on GitHub

Project Notes

php include

How to include or require other files in a PHP script

Notes

The include and require functions are used to include other source files in the current script. These functions are identical except on failure:

  • require will throw a fatal E_COMPILE_ERROR
  • include will emit a warning E_WARNING

Cascading Includes

Inclusions can be cascaded. In this example, a variable and constant is dinfed in a file indirectly included in the example: See example1

$ php example1/example.php
Running: example.php
CALLED: A $adjective $color $fruit
RESULT: A
DEFAULT_FRUIT: DEFAULT_FRUIT
Now including f_inner.php
Included: f_inner.php
f_inner defining : $color and $fruit
Included: f_inner_inner.php
f_inner_inner.php defining $adjective and DEFAULT_FRUIT
CALLED: A $adjective $color $fruit
RESULT: A juicy green pear
DEFAULT_FRUIT: apple

Include Paths

Unless absolute paths are specified for the

When evaluating a include/require function call, PHP will look in these locations:

  • the absolute file path given
  • under theinclude path
  • calling script’s own directory
  • current working directory
  • else fail

Credits and References

About LCK#230 php
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.