Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
The include and require statements
3.
include vs. require
4.
Example: Basic include
5.
Example: Including within functions
6.
Example: include through HTTP
7.
Example: Comparing return value of include
8.
Example: include and the return statement
9.
Example: Using output buffering to have a PHP include file into a string
10.
Frequently Asked Questions
11.
Key Takeaways
Last Updated: Mar 27, 2024

PHP Include Files

Author Abhay Trivedi
0 upvote

Introduction

PHP allows you to include files to reuse page content many times. It is beneficial to have files to apply HTML or PHP code to multiple website pages. The include or require statements takes all the text or code in the specified file and copy it into the file that uses these statements. Therefore, using the concept of file inclusion helps PHP include files in various programs and saves the effort of writing code multiple times.

The include and require statements

To insert the contents of one PHP file into another PHP file, we have to include the file with include or require statements. The include and require statements are identical in use. The difference is that when the include encounters an error, it will only produce a warning and continue to execute the script. And when a failure occurs while using the require, it makes a fatal error and stops the script.

Syntax:

include 'fileName'; // generates a warning & continue the execution.
or
require 'fileName'; // generates a fatal error & stops the execution.
You can also try this code with Online PHP Compiler
Run Code

include vs. require

If you are creating a PHP project and want the execution to go on, even if the included files may be missing, use the include statement since it will just give a warning and continue the file's execution.

If you are working on a complex PHP application like framework or CMS, and you want that the essential files must be present during the code execution. Always use the require statement since it will stop the execution and produce a fatal error in case of accidental missing of some vital files.

Must Read PHP Projects With Source Code

Example: Basic include

Let's assume we have a file called "dream.php" with a quote by Martin Luther King Jr.

<?php
  $dream = 'dream that my four little children will one day live in a nation where they will not be judged by the color of their skin, but by the content of their character.';
  // ~ Martin Luther King Jr.
?>
You can also try this code with Online PHP Compiler
Run Code

Let's assume that some error occurred while including the above file.

Using include:

<html>
<body>
<h1>Welcome to Coding Ninjas!</h1>
<?php include 'dream.php';
  echo "I have a $dream.";
?>
</body>
</html>
You can also try this code with Online PHP Compiler
Run Code

Output:

Using require:

<html>
<body>
    
<h1>Welcome to Coding Ninjas!</h1>
<?php require 'dream.php';
  echo "I have a $dream.";
?>

</body>
</html>
You can also try this code with Online PHP Compiler
Run Code

Output:

Example: Including within functions

The dream.php is in the scope of fun(), so $dream is NOT available outside this scope. $anotherDream is available because we declared it as global.

<?php
function fun(){
    global $anotherDream;
    $anotherDream = “big dragon inside a small washing machine.” ;
    include 'dream.php';
    echo "I have a $dream \n";
}

fun();
echo "A $anotherDream";
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Example: include through HTTP

This example assumes that www.example.com can parse .php files and not .txt files. 'Works' here means that the variable $dream is available within the included file.

<?php
  // Won't work since file.txt isn't handled by www.example.com
  include 'http://www.example.com/file.txt?dream=1&anotherdream=2';

  // Won't work since it looks for a file named 'file.php?dream=1&anotherdream=2' in the local filesystem.
  include 'file.php?dream=1&anotherdream=2';

  // Works
  include 'http://www.example.com/file.php?dream=1&anotherdream=2';
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Example: Comparing return value of include

Below is an example that compares the return value of the include statement.

<?php
  // won't work since it is evaluated as include(('dream.php') == TRUE), i.e., include('1')
  if (include('dream.php') == TRUE) {
      echo 'works';
  }

  // works
  if ((include 'dream.php') == TRUE) {
      echo 'works';
  }
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Example: include and the return statement

Here, we will see whether we can return a value in PHP include files.

return.php

<?php
  $dream1 = 'PHP is love';
  return $var;
?>
You can also try this code with Online PHP Compiler
Run Code

noreturn.php

<?php
  $dream2 = 'PHP is love with no returns';
?>
You can also try this code with Online PHP Compiler
Run Code

test.php

<?php
  $var = include 'return.php';
  echo $var; // prints 'PHP is love'
  $var1 = include 'noreturn.php';
  echo $var1; // prints 1
?>
You can also try this code with Online PHP Compiler
Run Code

Output:

Example: Using output buffering to have a PHP include file into a string

Use this method to include files within scripts automatically. Since this is a language construct and not a function, please don't call it using variable functions.

<?php
   $string = get_include_contents('dream.php');

   function get_include_contents($fileName) {
      if (is_file($fileName)) {
         ob_start();
         include $fileName;
         return ob_get_clean();
      }
      return false;
   }
?>
You can also try this code with Online PHP Compiler
Run Code

Frequently Asked Questions

1. What is PHP?

PHP is a scripting language used for backend web development. It is an open-source, decrypted server-side scripting language and supports object-oriented programming.

2. What are the advantages of using PHP?

There are many benefits of using PHP over other scripting languages. PHP is a server-side language suited for creating dynamic webpages. It is highly adaptable and provides compatibility and effortless integration. It supplies efficiency in performance and is cost-efficient. 

3. Does reading PHP include files, slow down PHP script load?

PHP Include Files is similar to import in Java and python in that we use it for class and function definitions. Include should be extremely fast, but using it will delay script execution than if it was not there.

Key Takeaways

This article teaches about PHP Include Files and how we use them. We saw why PHP Include Files could be beneficial for a developer to learn. Click here to read about PHP Interview Questions.

Click here to see other related blogs on PHP.

Also, check out our web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Happy Learning!

Live masterclass