PHP Functions

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Inbuilt Functions

  • include():-The include() function is used to include a file to another file.
  • include_onnce:-If the code form a file has been already included then it will not be include again,if we use include_once().
    include_once() include the file only once at a time.
  • Requie():-is same as Include()
  • Requie_once():-is same as include_once()
  • isset()
  • 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.

    include Statement

  • include will only produce a warning (E_WARNING) and the script will continue
  • If you want the execution to go on and show users the output, even if the include file is missing, use the include statement.

    Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.


    Syntax

    include 'filename';


    Assume we have a standard footer file called "footer.php", that looks like this:

    <?php
    echo "<p>Copyright &copy; 1999-" . date("Y") . " php.com</p>";
    ?>

    To include the footer file in a page, use the include statement:

    Example

    <html>
    <body>

    <h1>Welcome to my home page!</h1>
    <p>Some text.</p>
    <p>Some more text.</p>
    <?php include 'footer.php';?>

    </body>
    </html>

    Run Example>>



    require Statement

    The require statement is also used to include a file into the PHP code.

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • Syntax

    require 'filename';


    If we do the example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

    Example

    <html>
    <body>

    <h1>Welcome to my home page!</h1>
    <?php require 'noFileExists.php';
    echo "I have a $color $car.";
    ?>


    </body>
    </html>

    Run Example>>

    Use require when the file is required by the application.

    Use include when the file is not required and application should continue when file is not found.