Jump to content

How Do I Include One Php Script In Another?


skyler_sdf

Recommended Posts

Functions:

include('/filepath/filename')


require('/filepath/filename')


include_once('/filepath/filename')


require_once('/filepath/filename')
include() and include_once() will merely generate a warning on failure, while require() and require_once() will cause a fatal error and termination of the script. include_once() and require_once() differ from simple include() and require() in that they will allow a file to be included only once per PHP script. Let's see an example:

//

<?php

//we have the: include_me.php

echo 'I\ 've been included!<br />';

?>

Every time this script is included it will display the message "I've been included!", so we know it's worked. Now, let's test the various ways we can include this file in another script:

<?php

//This works fine

echo '<br />Requiring once: ';

require_once 'include_me.php';

//This works fine as well

echo '<br />Including: ';

include 'include_me.php';

//Nothing happens as file is already included

echo '<br />Includind once: ';

include_once 'include_me.php';

//This is fine

echo '<br />Requiring: ';

require 'include_me.php';

//Again nothing happens - the file is included

echo '<br />Requiring once again: ';

require-once 'include_me.php';

//Produces a warinig message as the file doesn't exist

echo '<br />Include the wrong file: ';

include 'include_wrong.php';

//Produces a fatal error and script execution halts

echo '<br />Requiring the wrong file: ';

require 'include_wrong.php';

//This will never be executed as we have a fatal error

echo '<br />Including again: ';

include 'include_me.php';

?>

The result will be sth like this:

Requiring once: I've been included

Including: I've been included

Including once:

Requiring: I've been included

Requiring once:

Include the wrong file:

Warning: Failed opening 'inlcude_wrong.php' for inclusion

Requiring the wrong file: Fatal error: Failed opening required 'include_wrong.php'

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.