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 of file myfile.php is:

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

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

php require function

PHP require() Syntax

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

require 'file';

Or

require('file');

Unlike include(), upon failure, the require() 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 exists.

Why to Use require() in PHP?

I do not know about your purpose to use 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 = "codescracker";
   
   $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 above example, should be:

php require function file

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

Note - The mysqli() 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/return the error code (if any) from last connect call, in object-oriented style.

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

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

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

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

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

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

Note - The close() 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 application written in PHP. That is, if the key file is missing, then the entire script does not gets executed.

PHP Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube