PHP include – How to Use It, With Examples

How to use PHP include

The PHP include expression allows you to import code from another file and import code from external PHP libraries. Here’s how to use it, with examples.

PHP include will execute and import PHP code from another file or library.  It allows you to re-use code and work with libraries provided by others – for example, you may download a library to interact with a mapping service and wish to include it in your code.

Syntax

include './path/to/file.php';

Simple! The include expression must be followed by the path to another PHP file, wrapped in quotes.

The path will be processed as follows:

  • If it is a full path, starting with a drive letter or \ or /, the full path will be used as read.
  • If the path begins with a . or .., it will be treated as a relative path
  • If neither of these conditions is met, the include_path variable from the PHP configuration will be used

Scope Inheritance and include

The code contained in the imported file will inherit the scope where the include expression was used.

For example, if the include expression is used at the top of a PHP file, the variables, functions, and code within it will be executed and available within the script’s global scope, which includes it.

If the include expression is used within a function, the imported code and any variables will only be executed and available within that function.

PHP include Expression Examples

This example will demonstrate two PHP files – The first file will contain some variables and will print a line to show it was executed when included, and the second file will include it:

animal.php

<?php

    echo "This is animal.php talking \n"; // \n will print a new line

    $size = "small";
    $animal = "dog";

?>

test.php

<?php

    include './animal.php';

    echo "There is a $size $animal";

?>

This will output the following:

This is animal.php talking 
There is a small dog

The code in the included animal.php is executed normally (hence the “talking” line is printed).

The variables defined in animal.php are available from test.php (hence “There is a” line is printed using variables from animals.php).

Scope Example

This example will re-use animal.php from above and show how include is affected by scopes:

scopeTest.php

<?php

function myFunction(){
    include './animal.php';
    echo "There is a $size $animal";
}

myFunction();

echo "There is a $size $animal, again";

?>

As you can see, the variables provided by animal.php are only available from within the function where they were included – they are undefined outside of it.

The Similar PHP require Expression

The require function is functionally identical to include; however, if it fails, it will raise an E_COMPILE_ERROR and halt the execution of your PHP code, whereas include will just produce a warning.

For more examples, you can check out the official PHP include documentation here.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *