PHP Include and Require commands

PHP Include and Require commands

PHP Include and Require commands


Include and Require commands take a file and insert that file contents into the script that issued the include command. These commands are quite useful when you want to include the same PHP, HTML, or text segment on multiple pages of a website.

<?php 
  
   include "path to file" ;
   // Syntax of include command

   require "path to file" ;
   // Syntax of require command
?>

Files are included on the basis of the path of the file, or if the path is not specified, the path specified in the directive include_path.

include_path

Specifies a list of directories in php.ini, in which the functions require (), include (), fopen (), file (), readfile () and file_get_contents () looking for files. When searching for include files PHP separately considers each value the include_path. It checks the first path if the file is not found there, it moves to the next, and so on until it finds an include file, or returns a warning or error.

Example # 1 include_path on Unix

include_path = ".: / PHP / includes"

Example # 2 include_path in Windows

include_path = ".; c: \ PHP \ includes"

If the file is not found in the include_path, commands will attempt to check the directory in which the current script and include the current working directory.

// header.php
<?php
   echo "<h1> Welcome to web.com</h1>";
?>

// index.php

<?php
   include "header.php";

  // or 

   require "header.php";
?>

There is one big difference between include and require; when a file is included with the include statement and PHP cannot find it the script will continue to execute, but when file is included with require statement and PHP cannot find it returns a fatal error and the script execution is stopped.


Reactions

Post a Comment

0 Comments

close