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

How to return ascii value of character in php?

0 Answers  


What are the delimiters in php?

0 Answers  


Why php language is used?

0 Answers  


What is the functionality of md5 function in php?

0 Answers  


if you run the app program all vendor open items are cleared but is it possible to reverse the again again open items please tell me the answer

0 Answers   Yash Technologies,






Which variable declarations within a class is invalid in php?

0 Answers  


How do I run a php script?

0 Answers  


Why laravel is the best php framework in 2019?

0 Answers  


What is "print" in php?

0 Answers  


Explain about the $_GET variable of PHP?

0 Answers  


What do you mean range() in php?

0 Answers  


What is the use of stripslashes in php?

0 Answers  


Categories