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
Why ide is recommended for use while programming with php?
Is session a cookie?
How to include variables in double-quoted strings in php?
Explain the differences between get and post methods?
How do sessions work in php?
What does the expression exception::__tostring means?
Is php a float?
What is the most convenient hashing method to be used to hash passwords?
How does api connect to database?
What is the difference between == and === in php?
How to delete file in php?
How cookies are trported from browsers to servers?
What is PHP's configuration file called?
How many types of arrays are there in php?
What is namespaces in PHP?