Include And Require In PHP
INCLUDE AND REQUIRE METHODS IN PHP

PHP include and require

  • In PHP, the include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
  • In PHP, Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
  • In PHP, It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

  • In PHP, Require() will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • In PHP, Include() will only produce a warning (E_WARNING) and the script will continue

Include & Require In PHP

  • In PHP, Use require() when the file is required by the application.
  • In PHP, Use include() when the file is not required and application should continue when file is not found.

Source Code

Index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Home Page</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>

<body>
    
    
    <div class="container">
    
    <div class="row">
    
        <?php require('include/header.php'); ?>
        
    </div>
        
    <div class="row mt-2">
    
        
        
            <?php require('include/navbar.php'); ?>
        
        
        
    </div>
    
        <div class="row">
        
            <div class="col-md-12">
            
                <h1>This Is Home Page</h1>
            
            </div>
        
        </div>
    
        <div class="row">
        
        <div class="col-md-12">
        
            <?php require('include/footer.php'); ?>
            
        </div>
        
        </div>
    
    </div>
    
<script src="js/bootstrap.min.js"></script>
</body>
</html>

AboutUs.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>About Page</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>

<body>
    
    
    <div class="container">
    
    <div class="row">
    
        <?php include('include/header.php'); ?>
        
    </div>
        
    <div class="row mt-2">
    
        
        
            <?php include('include/navbar.php'); ?>
        
        
        
    </div>
    
        <div class="row">
        
            <div class="col-md-12">
            
                <h1>This Is About Page</h1>
            
            </div>
        
        </div>
    
        <div class="row">
        
        <div class="col-md-12">
        
            <?php include('include/footer.php'); ?>
            
        </div>
        
        </div>
    
    </div>
    
<script src="js/bootstrap.min.js"></script>
</body>
</html>

ContactUs.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Contact Page</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>

<body>
    
    
    <div class="container">
    
    <div class="row">
    
        <?php include('include/header.php'); ?>
        
    </div>
        
    <div class="row mt-2">
    
        
        
            <?php include('include/navbar.php'); ?>
        
        
        
    </div>
    
        <div class="row">
        
            <div class="col-md-12">
            
                <h1>This Is Contact Page</h1>
            
            </div>
        
        </div>
    
        <div class="row">
        
        <div class="col-md-12">
        
            <?php include('include/footer.php'); ?>
            
        </div>
        
        </div>
    
    </div>
    
<script src="js/bootstrap.min.js"></script>
</body>
</html>

Header.php


<div class="text-center bg-dark text-white p-4">

    <h1>My New Header Of Website</h1>

</div>

navbar.php – Bootstrap 4 Navbar

<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Logo</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="collapsibleNavbar">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="Index.php">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="AboutUs.php">About</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="ContactUs.php">Contact</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

footer.php

<div class="text-center bg-dark text-white p-2">

    <h1>My New Footer Of Website</h1>

</div>

Download Notes Of This Blog From the link given below

https://www.mediafire.com/file/6dxyc54xvj7ea0s/INCLUDE+AND+REQUIRE+METHODS+IN+PHP.pptx/file

Download Source Code Of This Blog From the link given below

https://www.mediafire.com/file/m0gz0zudqhzwqgb/Website.rar/file

No responses yet

Leave a Reply

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