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

Answers were Sorted based on User's Feedback



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

Answer / guest

include includes the called file as many times as you call
include and include_once calls only once

Is This Answer Correct ?    12 Yes 3 No

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

Answer / 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

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

Answer / murali

include_once() will not include a file that has already been
included. include() will include a file every time.
include_once() is useful if you have php scripts which
include scripts which include scripts. If 2 of the scripts
include the same file then you might get variables and other
things being overwritten. Using include_once() ensures you
get the functionality that the scripts need but without any
side effects.

Is This Answer Correct ?    1 Yes 0 No

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

Answer / jamseer_syed

include_once() will useful for include the files many times
in the same program.
but the include() is useful for include the file only once
in the program otherwise if we use more then once it shows
error...

Is This Answer Correct ?    2 Yes 1 No

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

Answer / nikunj

when page is executed then include() func. include file
every times and include_once() func. include file only once
when page is execute

Is This Answer Correct ?    2 Yes 6 No

Post New Answer

More PHP Interview Questions

What is whitespace in php?

0 Answers  


What is the difference between md5(), crc32() and sha1() crypto on php?

0 Answers  


How to communicate with sockets in php?

1 Answers  


What is return value in php?

0 Answers  


What is php and what does it do?

0 Answers  






Why is php used for web development?

0 Answers  


Php says that an array is an ordered map. But how the values are ordered in an array?

0 Answers  


What is indexing in mysql and how do we create indexing in mysql

4 Answers  


when we submit any string in single inverted comma('abc') with get or post method,we will get exact string with single inverted comma('abc')by using $_REQUEST[]. But it is giving (\'abc\'). so anybody please help me to remove that slash,therefore i will get exact string...

2 Answers  


Is php still used?

0 Answers  


How to turn on the session support in php?

0 Answers  


Is null check in php?

0 Answers  


Categories