PHP Includes: What is it?

· Dev & Design
Authors

include()

The include() function includes and evaluates the specified file.

The include() statement takes all the contents of the specified file and
copies it to the one which consist this statement.

vars.php
<?php
$color = ‘green’;
$fruit = ‘apple’;
?>

test.php
<?php
echo “A $color $fruit”; // A
include ‘vars.php’;
echo “A $color $fruit”; // A green apple
?>


require().

Similarly the require() statement includes and evaluates the specified file.
Both include() and require() works in same way, the only difference is how they handle
failure. The include() statement produces warning, but the script will continue its
execution. While the require() statement produces a Fatal Error and script stop its execution.

<html>
<body>
<?php
require(“wrongFile.php”);
echo “Hello World!”;
?>
</body>
</html>

Warning:
require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5

Fatal error:
require() [function.require]:
Failed opening required ‘wrongFile.php’
(include_path=’.;C:\php5\pear’)
in C:\home\website\test.php on line 5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s