Require statements in PHP

PHP require statement

  • The require statement is used to include a file into the PHP code.
  • Syntax
  • require 'filename';
    OR
    require_once 'filename';

  • Example-1

  • Main.php file code
  • <html>
    <body>
    <p>Main file content</p>
    <?php
    require 'include_file.php';
    ?>

    </body>
    </html>

  • include_file.php file code
  • <?php
    echo "<p>Require file content</p>";
    ?>

    Output:

    Main file content
    Include file content

  • Example-2

  • Main.php file code using include_once()
  • <html>
    <body>
    <?php
    require_once 'include_file.php';
    echo "Main File content :- Welcome to $name.";
    ?>

    </body>
    </html>



  • include_file.php file code
  • <?php
    $name='PHPCodersPoint';
    ?>

    Output:

    Main File content :- Welcome to PHPCodersPoint.