PHP require() function

The PHP require() function is used when we need to require a file to execute first, before the execution of PHP scripts after the require(). For example:

<?php
   require("myfile.php");
   
   echo "PHP is Fun!<BR>";
   echo "Is not it?";
?>

The script for the file myfile.php is:

<?php
   echo "This is text inside \"myfile.txt\" file<HR>";
?>

Now the output of the above PHP example on the require() function is shown in the snapshot given below:

php require function

PHP require() Syntax

The syntax of the require() function in PHP is:

require 'file';

Or

require('file');

Unlike include(), upon failure, the include() function produce a fatal error (E_COMPILE_ERROR), that halts the current PHP script to execute further. For example:

<?php
   echo "Before require()<BR>";
   
   require("unknown.php");
   
   echo "After require()<BR>";
?>

The output produced by this PHP example on require() is:

php require function example

See the output; the script before the require() function has been executed, whereas the script after the function has not been executed, as the specified file does not exist.

Why use require() in PHP?

I do not know about your purpose for using the require() function. But this function plays an important role when your application needs to execute some script (from an external file) before executing the current PHP script. For example:

<?php
   require 'dbConnection.php';
   
   $sql = "SELECT * FROM customer";
   
   $result = $conn -> query($sql);
   if($result)
   {
      while($row = $result -> fetch_row())
      {
         echo "Name: ", $row[1];
         echo "<BR>";
         echo "Age: ", $row[2];
         echo "<HR>";
      }
      $result -> free_result();
   }
   else
   {
      echo "Something went wrong!<BR>";
      echo "Error Description: ", $conn -> error;
   }
   $conn -> close();
?>

And here is the script of the file named dbConnection.php:

<?php
   $server = "localhost";
   $user = "root";
   $pass = "";
   $db = "fresherearth";
   
   $conn = new mysqli($server, $user, $pass, $db);
   
   if($conn -> connect_errno)
   {
      echo "Connection to the database failed!<BR>";
      echo "Reason: ", $conn -> connect_error;
      exit();
   }
?>

Now the output produced by the above example should be:

php require function file

That is, before a successful connection to the database, the script does not execute.

Note: The mysqli() function is used to open a connection to the MySQL database server, in object-oriented style.

Note: The new keyword is used to create a new object.

Note: The connect_errno is used to get or return the error code (if any) from the last connect call in object-oriented style.

Note: The connect_error is used to get the error description (if any) from the last connection in object-oriented style.

Note: The exit() function is used to terminate the execution of the current PHP script.

Note: The query() function is used to perform queries on the MySQL database in object-oriented style.

Note: The fetch_row() function is used to fetch and return the result as an enumerated array in object-oriented style.

Note: The free_result() function is used to free the stored result in object-oriented style.

Note: The error is used to return the description of the error (if any) by the most recent function call in object-oriented style.

Note: The close() function is used to close an opened connection in object-oriented style.

Note: The require() function is used most of the time to include key file(s) to avoid compromising the security of web applications written in PHP. That is, if the key file is missing, then the entire script does not get executed.

PHP Online Test


« Previous Tutorial Next Tutorial »