What's the diff. between include() and Include_once().

Answer Posted / sagar_joshi

The documentation below also applies to require(). The two
constructs are identical in every way except how they handle
failure. They both produce a Warning, but require() results
in a Fatal Error. In other words, use require() if you want
a missing file to halt processing of the page. include()
does not behave this way, the script will continue
regardless. Be sure to have an appropriate include_path
setting as well. Be warned that parse error in included file
doesn't cause processing halting in PHP versions prior to
PHP 4.3.5. Since this version, it does.

Files for including are first looked for in each
include_path entry relative to the current working
directory, and then in the directory of current script. E.g.
if your include_path is libraries, current working directory
is /www/, you included include/a.php and there is include
"b.php" in that file, b.php is first looked in
/www/libraries/ and then in /www/include/. If filename
begins with ./ or ../, it is looked for only in the current
working directory or parent of the current working
directory, respectively.

When a file is included, the code it contains inherits the
variable scope of the line on which the include occurs. Any
variables available at that line in the calling file will be
available within the called file, from that point forward.
However, all functions and classes defined in the included
file have the global scope.

Example #1 Basic include() example
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

?>

********and in include_once is as below**********

he include_once() statement includes and evaluates the
specified file during the execution of the script. This is a
behavior similar to the include() statement, with the only
difference being that if the code from a file has already
been included, it will not be included again. As the name
suggests, it will be included just once.

include_once() may be used in cases where the same file
might be included and evaluated more than once during a
particular execution of a script, so in this case it may
help avoid problems such as function redefinitions, variable
value reassignments, etc.

for example::>

include_once() with a case insensitive OS in PHP 4
<?php
include_once "a.php"; // this will include a.php
include_once "A.php"; // this will include a.php again! (PHP
4 only)
?>

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to assigning a new character in a string using php?

744


What does a special set of tags do in php?

724


What is data structure in php?

729


Is php coding easy?

686


What is uri routing?

706


How to include a file to a PHP page?

754


How can you retrieve a cookie value?

745


Why session timeout is important?

834


Explain me what is the w3c?

779


What is meant by MIME?

808


What is htaccess? Why do we use this and where?

707


What is use of count() function in php?

761


Why json is used in php?

733


What is the $_ server php_self variable?

696


what is difference between PHP4 , PHP5

2096